---
description: 🚧 under construction
---

# using regex

[JS](../../..) ⟩ [value](../..) ⟩ [object](../../obj) ⟩ [regex]() ⟩ 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**](flag) 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;

* [numbers](ex/numbers "mention")
{% 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](../../prim/str/method/str.matchall "mention")
* [str.replace](../../prim/str/method/str.replace "mention")
* [replace-pattern](pattern/replace-pattern "mention")
{% 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 %}
