---
description: 🚧 under construction
---

# words in sentence

[JS](../../../../..) ⟩ [primitives](../../..) ⟩ [String](../..) ⟩ [methods](..) ⟩ [.matchAll()]() ⟩ words in sentence

{% hint style="success" %}
desc.
{% endhint %}

{% tabs %}
{% tab title="💾 程式" %}
:floppy\_disk: replit：[words in sentence](https://replit.com/@pegasusroe/matchAll-words-in-a-string#index.js)

```javascript
// ⭐️ 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'
```
{% endtab %}

{% tab title="📗 參考" %}
* [ ] JavaScript: The Definitive Guide ⟩ 11.3.2 String Methods for Pattern Matching
{% endtab %}

{% tab title="👥 相關" %}
* [](<> "mention") returns an [iterable](../../../../../iteration/iterator/iterable "mention").
* [regex](../../../../builtin/regex "mention")
{% endtab %}
{% endtabs %}
