# using regex

[JS](/web/js.md) ⟩ [value](/web/js/val.md) ⟩ [object](/web/js/val/obj.md) ⟩ [regex](/web/js/val/builtin/regex.md) ⟩ using regex

{% hint style="success" %}
Regular expressions are used &#x20;

* with [`re.test()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test), [`re.exec()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec)&#x20;
* with [`str.match()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match), [`str.replace()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace), [`str.search()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/search), [`str.split()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split).
  {% endhint %}

{% tabs %}
{% tab title="🧨 雷區" %}
{% hint style="danger" %}
when using [str.matchAll(regex)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/matchAll), the <mark style="color:blue;">`regex`</mark> <mark style="color:red;">**must**</mark>**&#x20;**<mark style="color:yellow;">**have**</mark> the <mark style="color:orange;">**global**</mark> (<mark style="color:yellow;">**`g`**</mark>) [**flag**](/web/js/val/builtin/regex/flag.md) set, or a [`TypeError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypeError) is thrown:exclamation:
{% endhint %}
{% endtab %}

{% tab title="⭐️ 重點" %}

<table><thead><tr><th width="215">Method</th><th>Return Value or Side Effect</th></tr></thead><tbody><tr><td><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec"><code>re.exec()</code></a></td><td>array of information or <code>null</code>.</td></tr><tr><td><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test"><code>re.test()</code></a></td><td><code>true</code> or <code>false</code>.</td></tr><tr><td><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match"><code>str.match()</code></a></td><td>array containing all of the matches (including capturing groups), or <code>null</code>.</td></tr><tr><td><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/matchAll"><code>str.matchAll()</code></a></td><td>iterator containing all of the matches (including capturing groups).</td></tr><tr><td><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/search"><code>str.search()</code></a></td><td>index of the match, or <code>-1</code>.</td></tr><tr><td><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace"><code>str.replace()</code></a></td><td>replaces the matched substring with a replacement substring.</td></tr><tr><td><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replaceAll"><code>str.replaceAll()</code></a></td><td>replaces the matched substrings with a replacement substring.</td></tr><tr><td><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split"><code>str.split()</code></a></td><td>break a string into an array of substrings.</td></tr></tbody></table>
{% endtab %}

{% tab title="💈範例" %}

```javascript
/abc/.test("xxabcde"),    // true
/abc/.test("xxabxde"),    // false
```

💈範例：&#x20;

* [JS-style numbers](/web/js/val/builtin/regex/ex/numbers.md)
  {% endtab %}

{% tab title="👥 相關" %}

* [str.indexOf()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf) can search for substrings too.
* [str.matchAll()](/web/js/val/prim/str/method/str.matchall.md)
* [str.replace()](/web/js/val/prim/str/method/str.replace.md)
* [replace pattern](/web/js/val/builtin/regex/pattern/replace-pattern.md)
  {% endtab %}

{% tab title="📗 參考" %}

* [ ] Eloquent JavaScript ⟩ Regular Expressions ⟩ [Testing for Matches](https://eloquentjavascript.net/09_regexp.html#h_vPyyYjMEtz)&#x20;
  {% endtab %}

{% tab title="📘 手冊" %}

* [regex.test()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test) - test if a match exists.
* [regex.exec()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec) - use str.matchAll() instead.
* [str.search()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/search) - index of first match, -1 otherwise.
* [str.matchAll()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/matchAll) - contents of matches.
  {% 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/builtin/regex/use.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.
