How to use the words function from lodash

Find comprehensive JavaScript lodash.words code examples handpicked from public code repositorys.

lodash.words is a function in the Lodash library that splits a string into an array of words.

432
433
434
435
436
437
438
439
440
441
module.exports.valuesAt            = _.valuesAt;
module.exports.valuesIn            = _.valuesIn;
module.exports.walk                = _.walk;
module.exports.weave               = _.weave;
module.exports.without             = _.without;
module.exports.words               = _.words;
module.exports.wrap                = _.wrap;
module.exports.xor                 = _.xor;
module.exports.xorBy               = _.xorBy;
module.exports.xorWith             = _.xorWith;
fork icon19
star icon122
watch icon0

+ 92 other calls in file

871
872
873
874
875
876
877
878
879
880
881
882
883
884
console.log(upperCase); // => 'FOO BAR'


const upperFirst = _.upperFirst('fred');
console.log(upperFirst); // => 'Fred'


const words = _.words('fred, barney, & pebbles');
console.log(words); // => ['fred', 'barney', 'pebbles']




// Util
fork icon0
star icon4
watch icon0

+ 15 other calls in file

How does lodash.words work?

lodash.words is a function in the Lodash library that splits a string into an array of words. The function takes two optional arguments: a string to split and an optional pattern to match against the string. If no pattern is provided, the default pattern matches any whitespace characters. The function returns an array of words extracted from the input string, based on the specified pattern. Each element of the resulting array is a string representing a word in the input string. For example, the following code uses lodash.words to split a string into an array of words: javascript Copy code {{{{{{{ const _ = require('lodash'); const text = 'The quick brown fox jumps over the lazy dog'; const words = _.words(text); console.log(words); // Output: [ 'The', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog' ] In this example, we use lodash.words to split the string text into an array of words. The resulting words variable contains the array [ 'The', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog' ]. Overall, lodash.words is a useful utility function for splitting a string into an array of words, based on a specified pattern.

42
43
44
45
46
47
48
49
50
51
})

createNodeField({
  node,
  name: "wordCount",
  value: _.words(node.rawMarkdownBody, /[\s\p{sc=Han}]/gu).length
})

// Use `createFilePath` to turn markdown files in our `data/faqs` directory into `/faqs/slug`
const file = findFileNode({node, getNode});
fork icon0
star icon0
watch icon1

Ai Example

1
2
3
4
5
6
7
const _ = require("lodash");

const text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
const words = _.words(text, /[^\s,;]+/g);

console.log(words);
// Output: [ 'Lorem', 'ipsum', 'dolor', 'sit', 'amet', 'consectetur', 'adipiscing', 'elit.' ]

In this example, we use lodash.words to split the string text into an array of words, based on a regular expression pattern [^\s,;]+ that matches one or more characters that are not whitespace, comma, or semicolon. The resulting words variable contains the array [ 'Lorem', 'ipsum', 'dolor', 'sit', 'amet', 'consectetur', 'adipiscing', 'elit.' ], which represents the words in the input string. Overall, lodash.words is a useful utility function for splitting a string into an array of words, based on a specified pattern.

Other functions in lodash

Sorted by popularity

function icon

lodash.get is the most popular function in lodash (7670 examples)