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'