---
description: Regular Expressions
---

# RegExp

[JS](../..) ⟩ [value](..) ⟩ [object](../obj) ⟩ 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**](../prim/str) ⟩ [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**](../prim/str/unicode/encode/utf16/code-unit), <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**](regex/pattern/emoji)) behave strangely.
* Enable "<mark style="color:yellow;">`/u`</mark>" [**flag**](regex/flag) to support [**Unicode**](../prim/str/unicode) 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>
  * [create](regex/create "mention")
* <mark style="color:yellow;">**regex properties**</mark>
  * [flag](regex/flag "mention")
  * [lastindex](regex/lastindex "mention")
* <mark style="color:yellow;">**pattern matching**</mark>
  * [use](regex/use "mention")
  * [pattern](regex/pattern "mention")
* :sparkles: [<mark style="color:yellow;">**examples**</mark>](regex/ex)
{% endtab %}

{% tab title="👥" %}
* [str.replace](../prim/str/method/str.replace "mention")
* [str.replacewhitespaces](../prim/str/method/str.replacewhitespaces "mention")
{% 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 %}
