words in sentence

🚧 under construction

JSprimitivesStringmethods.matchAll() ⟩ words in sentence

desc.

💾 replit:words in sentence

// ⭐️ Unicode "alphabetic characters" between word boundaries 
const word = /\b\p{Alphabetic}+\b/gu;     // \p is not supported in Firefox yet❗

const text = `
    This is a naïve test of the matchAll() method.
    這裡中文字
    ✏️ うわ、喧嘩してる!何かあったんですか?
`; // ⭐️ Chinese and Japanese characters are NOT "alphabetic characters"❗

// ⭐️ (iterable) iterator of all matches (words)
const matches = text.matchAll(word);

// iterate using for-of loop
for(let match of matches) { 
    console.log(`${(match.index + '').padStart(3, '0')}: '${match[0]}'`);
    //              ╰──index──╯                             ╰─word─╯
}

// output
// --------------
// 005: 'This'
// 010: 'is'
// 013: 'a'
// 015: 'naïve'
// 021: 'test'
// 026: 'of'
// 029: 'the'
// 033: 'matchAll'
// 044: 'method'

Last updated