How to use the words function from underscore.string

Find comprehensive JavaScript underscore.string.words code examples handpicked from public code repositorys.

underscore.string.words is a JavaScript function in the Underscore.string library that splits a string into an array of words, with customizable delimiters and filters.

76
77
78
79
80
81
82
83
84
85
86
    body: body
  };
}


function _parseFirstLine(line) {
  var methodUrlProtocolVer = _s.words(line, ' ');
  if (methodUrlProtocolVer.length !== 3) {
    throw new InvalidRequestError('First line must have format: [Method] SP [Url] SP [Protocol]', line);
  }

fork icon1
star icon0
watch icon0

+ 39 other calls in file

125
126
127
128
129
130
131
132
133
134
135
136
    res.set("Content-Type: text/plain");
    res.send(response);
});


function split(inputText) {
    let result = underscore.words(inputText, /\*/);


    return result;
}

fork icon0
star icon0
watch icon1

How does underscore.string.words work?

underscore.string.words works by splitting a string into an array of words, with customizable delimiters and filters. When called with a string as its argument, underscore.string.words splits the string into an array of words, using whitespace as the default delimiter. The resulting array contains each word in the string, in the order they appear, with any leading or trailing whitespace removed. The delimiter used to split the string can be customized using the delimiter argument, which can be a regular expression, a string, or a function that returns a regular expression or string. The delimiter can also be omitted, in which case the default whitespace delimiter is used. The resulting array of words can be further filtered or processed using the filter argument, which can be a regular expression, a string, or a function that returns a Boolean value. The filter can be used to exclude or include specific words in the array, based on their content or length. The resulting array of words can also be mapped or reduced using other Underscore.js or Underscore.string functions, such as _.map, _.reduce, or _.pluck. Note that underscore.string.words is part of the Underscore.string library, which extends the functionality of the Underscore.js library with various string manipulation and formatting functions, and provides a more convenient and readable API for working with strings in JavaScript.

156
157
158
159
160
161
162
163
164
165
166
167
// Last, N.N.N.
// and should be translated into N.(N.N.) Last


function fix_author(a) {
  let name = a.name;
  let components = $.words(name, ",");
  let first_name = $.capitalize($.clean(components[1]), true);
  let last_name = $.capitalize($.clean(components[0]), true);


  if (_.isPlainObject(patt[last_name])) {
fork icon0
star icon0
watch icon2

Ai Example

1
2
3
4
5
6
7
8
9
const _ = require("underscore");
require("underscore.string");

const str = "The quick brown fox jumps over the lazy dog";

const words = _.words(str);

console.log(words);
// Output: ["The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"]

In this example, we first import the Underscore.js and Underscore.string libraries, and define a string str containing a sentence with several words. We then call the _.words function with str as its argument, which splits the string into an array of words using the default whitespace delimiter. The resulting array of words is then printed to the console. The output will be an array containing the words in the original sentence, in the order they appear, with any leading or trailing whitespace removed. Note that this is just a simple example, and underscore.string.words can be used to split strings with various delimiters and filters, and can be combined with other Underscore.js or Underscore.string functions to perform more complex string manipulations or transformations.