> For the complete documentation index, see [llms.txt](https://lochiwei.gitbook.io/web/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lochiwei.gitbook.io/web/js/val/builtin/regex.md).

# RegExp

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

{% hint style="success" %} <mark style="color:purple;">**Regular expressions**</mark> are string <mark style="color:yellow;">**patterns**</mark> and used with：

* <mark style="color:purple;">**RegExp**</mark> ⟩  [.exec()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec) , [.test()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test).&#x20;
* [**String**](/web/js/val/prim/str.md) ⟩ [match()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match), [matchAll()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/matchAll), [replace()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace), [replaceAll()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replaceAll), [search()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/search), [split()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split).

```regex
\w    word character    = [a-zA-Z0-9_]
\s    whitespace        
```

:point\_right: [regular expression cheatsheet](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Cheatsheet)
{% endhint %}

{% tabs %}
{% tab title="🧨" %}
{% hint style="danger" %}

* By default, <mark style="color:purple;">**regular expressions**</mark> work on [**code units**](/web/js/val/prim/str/unicode/encode/utf16/code-unit.md), <mark style="color:red;">**not**</mark>**&#x20;**<mark style="color:yellow;">**actual characters**</mark>:exclamation:
* Characters that are composed of **two code units** (e.g. [**emojis**](/web/js/val/builtin/regex/pattern/emoji.md)) behave strangely.
* Enable "<mark style="color:yellow;">`/u`</mark>" [**flag**](/web/js/val/builtin/regex/flag.md) to support [**Unicode**](/web/js/val/prim/str/unicode.md) in **regular expressions**.
  {% endhint %}
  {% endtab %}

{% tab title="⭐️" %}
{% hint style="warning" %}
JavaScript [`RegExp`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp) objects are <mark style="color:red;">**stateful**</mark> when they have the [`global`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/global) or [`sticky`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/sticky) flags set (e.g. /foo/<mark style="color:red;">**g**</mark> or /foo/<mark style="color:red;">**y**</mark>). They store a [`lastIndex`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/lastIndex) from the previous match.&#x20;
{% endhint %}

{% hint style="success" %}
•  <mark style="color:red;">**test**</mark> if a string matches a regex:  [regex.test()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test)

•  find <mark style="color:red;">**index**</mark> position in the string:  [str.search()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/search)&#x20;

•  only want the <mark style="color:red;">**first match**</mark>:  [regex.exec()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec) or [str.match()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match) with <mark style="color:red;">**/g not set**</mark>.

•  return <mark style="color:red;">**all matches**</mark>:  [str.match()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match)&#x20;

•  obtain <mark style="color:red;">**capture groups**</mark> (with <mark style="color:red;">**/g**</mark> set):  [regex.exec()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec)  or  [str.matchAll()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/matchAll) ⭐️&#x20;
{% endhint %}
{% endtab %}

{% tab title="🔴" %}

* <mark style="color:yellow;">**creating regex**</mark>
  * [creating regex](/web/js/val/builtin/regex/create.md)
* <mark style="color:yellow;">**regex properties**</mark>
  * [regex flag](/web/js/val/builtin/regex/flag.md)
  * [lastIndex](/web/js/val/builtin/regex/lastindex.md)
* <mark style="color:yellow;">**pattern matching**</mark>
  * [using regex](/web/js/val/builtin/regex/use.md)
  * [pattern](/web/js/val/builtin/regex/pattern.md)
* :sparkles: [<mark style="color:yellow;">**examples**</mark>](/web/js/val/builtin/regex/ex.md)
  {% endtab %}

{% tab title="👥" %}

* [str.replace()](/web/js/val/prim/str/method/str.replace.md)
* [str.replaceWhitespaces()](/web/js/val/prim/str/method/str.replacewhitespaces.md)
  {% endtab %}

{% tab title="🛠" %}

* [Debuggex](https://www.debuggex.com/) - to "see" regex.
* [RegExr](https://regexr.com)  ⭐️
* Unicode Utilities: [Character Properties](https://util.unicode.org/UnicodeJsps/character.jsp)
  {% endtab %}

{% tab title="📗" %}

* [ ] [Regular Expression Quick Reference](http://regexrenamer.sourceforge.net/help/regex_quickref.html)
* [ ] JS.info ⟩ [Regular expressions](https://javascript.info/regular-expressions)
* [ ] Eloquent JavaScript ⟩&#x20;
  * [ ] [Regular Expressions](https://eloquentjavascript.net/09_regexp.html)
  * [ ] [Parsing .ini file](https://eloquentjavascript.net/09_regexp.html#h_RGsf6ah1EY)
* [ ] RIP Tutorial ⟩ [Regular Expressions](https://riptutorial.com/regex)
* [ ] [Simple Markdown Parser with JavaScript and Regular Expressions](https://www.bigomega.dev/markdown-parser)
  {% endtab %}

{% tab title="📘" %}

* [ ] [JavaScript Guide](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide) ⟩ [Regular expressions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions)
* [ ] [References](https://developer.mozilla.org/en-US/docs/Web) ⟩ [Regular expressions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions)
* [ ] [RegExp](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp)
  {% endtab %}
  {% endtabs %}
