# words in sentence

[JS](https://lochiwei.gitbook.io/web/js) ⟩ [primitives](https://lochiwei.gitbook.io/web/js/val/prim) ⟩ [String](https://lochiwei.gitbook.io/web/js/val/prim/str) ⟩ [methods](https://lochiwei.gitbook.io/web/js/val/prim/str/method) ⟩ [.matchAll()](https://lochiwei.gitbook.io/web/js/val/prim/str/method/str.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="👥 相關" %}

* [](https://lochiwei.gitbook.io/web/js/val/prim/str/method/str.matchall "mention") returns an [iterable](https://lochiwei.gitbook.io/web/js/iteration/iterator/iterable "mention").
* [regex](https://lochiwei.gitbook.io/web/js/val/builtin/regex "mention")
  {% endtab %}
  {% endtabs %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://lochiwei.gitbook.io/web/js/val/prim/str/method/str.matchall/words-in-sentence.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
