> 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/pattern/char-set.md).

# character set

🚧 under construction

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

{% hint style="success" %}

```regex
.	// any non-newline character 
[^]     // any character (including newline) ⭐️

[a-c]   // (ranges) = [abc] = (a|b|c)
[^01]   // invert a set (not 0, not 1 in this case)

\d	// a digit = [0-9] = [0123456789]
\D	// not a digit
\w	// alphanumeric character (“word character”) = [a-zA-Z0-9_]
\W	// nonalphanumeric character
\s	// whitespace (space, tab, newline, ...)
\S	// nonwhitespace
```

:u6307: <mark style="color:yellow;">**synonyms**</mark>： "<mark style="color:purple;">**character class**</mark>"
{% endhint %}

{% tabs %}
{% tab title="⭐️ 重點" %}
{% hint style="warning" %}
Only <mark style="color:yellow;">`^ - \ ]`</mark> need to be escaped inside a <mark style="color:purple;">**character class**</mark>.

```javascript
[.+]	// ".", "+" have NO special meaning in brackets
```

{% endhint %}

{% hint style="warning" %}

* As far as JavaScript’s regular expressions are concerned, a “<mark style="color:yellow;">**word character**</mark>” is only one of <mark style="color:yellow;">`A-Z`</mark>, <mark style="color:yellow;">`a-z`</mark>, <mark style="color:yellow;">`0-9`</mark>, and <mark style="color:yellow;">`_`</mark>.&#x20;
* Things like *é* or *β*, <mark style="color:yellow;">**will**</mark>**&#x20;**<mark style="color:red;">**not**</mark> match <mark style="color:blue;">`\w`</mark> (and <mark style="color:yellow;">**will match**</mark> <mark style="color:blue;">`\W`</mark>).
  {% endhint %}

{% hint style="info" %}
Within square brackets <mark style="color:yellow;">`[x-y]`</mark>, a hyphen (<mark style="color:yellow;">`-`</mark>) between two characters can be used to indicate a <mark style="color:yellow;">**range of characters**</mark>, where the <mark style="color:yellow;">**ordering**</mark> is determined by the character’s [**Unicode number**](/web/js/val/prim/str/unicode/code-point.md).&#x20;
{% endhint %}
{% endtab %}

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

```javascript
// difference set: [^Ab] = a - b, where a = not A.
/[^e\W]/     // word character, but not an 'e'.
```

{% endtab %}

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

* [ ] Eloquent JavaScript ⟩ Regular Expressions ⟩ [Sets of Characters](https://eloquentjavascript.net/09_regexp.html#h_8EFR0DU1xd)
  {% endtab %}

{% tab title="🗣 討論" %}

* [Regular Expressions: Is there an AND operator?](https://stackoverflow.com/a/67415321/5409815)
  {% endtab %}
  {% endtabs %}
