Toolbox-XToolbox-X

Count Words in String

Counts the number of words in a string, supporting multiple languages and scripts.

countWords

The countWords function counts the number of words in a string, using Unicode-aware patterns to accurately count words across different scripts and writing systems.

Function Signature

function countWords(text: string): number

Parameters

  • text (string): The input string to count words from.

Return Value

  • number: The number of word-like tokens found in the string.

Example Usage

playground.ts

Notes

  • This function is Unicode-aware, supporting letters with diacritics, punctuation like apostrophes, and hyphenated words.
  • Numeric sequences (e.g. "123") are counted as words.

Aliases

The following aliases can be used for the countWords function:

  • countWordsInString
  • wordCount

Last updated: Sun, Jun 14, 2026 07:06:16AM (UTC)

On this page