words()
words(
str,pattern?):string[]
Splits string into an array of its words.
Default pattern handles camelCase, PascalCase, snake_case, kebab-case, and mixed strings.
Parametersβ
str: stringβ
The string to inspect.
pattern?: string | RegExpβ
The pattern to match words (optional).
Returns: string[]β
The array of words.
Sinceβ
2.0.0
Noteβ
Includes Unicode letters and numbers in default pattern.
Performanceβ
O(n) where n is string length. Single regex pass.
Also known asβ
words (Lodash, es-toolkit) Β· β (Remeda, Radashi, Ramda, Effect, Modern Dash, Antfu)
Exampleβ
words('fred, barney, & pebbles');
// => ['fred', 'barney', 'pebbles']
words('fred, barney, & pebbles', /[^, ]+/g);
// => ['fred', 'barney', '&', 'pebbles']
words('camelCase');
// => ['camel', 'Case']
words('PascalCase');
// => ['Pascal', 'Case']
words('snake_case');
// => ['snake', 'case']
words('kebab-case');
// => ['kebab', 'case']
words('XMLHttpRequest');
// => ['XML', 'Http', 'Request']
How it works?β
Splits string into an array of its words. Handles camelCase, PascalCase, snake_case, kebab-case.
Case Detectionβ
Custom Patternβ
Use Casesβ
Parse variable names for display πβ
Convert code-style identifiers to readable words. Essential for auto-generating labels from API field names.
words('firstName'); // => ['first', 'Name']
words('XMLHttpRequest'); // => ['XML', 'Http', 'Request']
Build search indexes from content πβ
Extract searchable tokens from mixed-format content. Useful for building client-side search.
words('async-data in JavaScript_apps');
// => ['async', 'data', 'in', 'Java', 'Script', 'apps']
Slug generation from titlesβ
Create URL-friendly slugs from titles with any naming convention.
words('How to Use XMLHttpRequest').join('-').toLowerCase();
// => 'how-to-use-xml-http-request'