# Character Classes

<table><thead><tr><th>Function</th><th width="177.33333333333331">RegExp equivalent</th><th>Explanation</th></tr></thead><tbody><tr><td><code>word</code></td><td><code>\w</code></td><td>Match alphanumeric characters and underscore</td></tr><tr><td><code>not.word</code><br><code>not(word)</code></td><td><code>\W</code></td><td>Match anything other than <code>\w</code></td></tr><tr><td><code>digit</code></td><td><code>\d</code></td><td>Match a character between <code>0</code> to <code>9</code></td></tr><tr><td><code>not.digit</code><br><code>not(digit)</code></td><td><code>\D</code></td><td>Match anything other than a digit</td></tr><tr><td><code>whitespace</code></td><td><code>\s</code></td><td>Match all types of whitespace characters</td></tr><tr><td><code>not.whitespace</code><br><code>not(whitespace)</code></td><td><code>\S</code></td><td>Match anything other than a whitespace</td></tr><tr><td><code>char</code></td><td><code>.</code></td><td>Match any character other than newline (or including line terminators when single line flag is set)<br>Note: <code>not.char</code> does not exist because it does not match anything</td></tr><tr><td><code>charIn`a-z_-`</code><br><code>charIn('a-z', '_-')</code><br><code>charIn`a-z` `_-`</code></td><td><code>[a-z_-]</code></td><td>Match a character listed in the group. A hyphen denotes a range of characters, such as <code>a-z</code>. †</td></tr><tr><td><code>notCharIn`a-z_-`</code><br><code>not.charIn`a-z_-`</code><br><code>notCharIn('a-z', '_-')</code><br><code>notCharIn`a-z` `_-`</code></td><td><code>[^a-z_-]</code></td><td>Match a character not listed in the group. †</td></tr><tr><td><code>charRange`a` `z`</code><br><code>charRange('a', 'z')</code></td><td><code>[a-z]</code></td><td>Match a character ranging from the first char to the second one.<br>Escape sequences are supported and the two characters must be in order.</td></tr><tr><td><code>notCharRange`a` `z`</code><br><code>not.charRange`a` `z`</code><br><code>notCharRange('a', 'z')</code></td><td><code>[^a-z]</code></td><td>Match a character not in the range of first to second char.<br>Escape sequences are supported and the two characters must be in order.</td></tr></tbody></table>

#### † Notes on character classes

`charIn` accepts a list of strings and special sequences, but you can also combine the list into one string if you prefer:

* `charIn('a-z0-9' + whitespace)`
* `` charIn`a-z0-9${whitespace}` ``
* `` charIn`a-z0-9\s` ``

However, combining a list of options into one string is not equivalent to a simple string concatenation. `-` is escaped at the beginning and end of each string in the list, so `` charIn`a-` `z` `` matches `a`, `-` and `z` literally, while `` charIn`a-z` `` matches alphabets from `a` to `z`.

***

Apart from `-`, `^` and `]` are also escaped in the character class, so you cannot negate a `charIn` via a `^` character (you should use `notCharIn`), and you cannot close a character class prematurely.

Backslashes `\` are only escaped at the end of a string, so you can use escape sequences such as `\uffff` and `\xff` freely. If you want to include `\` in the character class, you should write it at the end of a string or escape with `\\`.

***

Additionally, `charIn` allows you to merge character classes by simply passing in a `charIn` or `charRange` token. For example:

```ts
const symbols = charIn`-_*$`; // the character class to be merged must not be negated (cannot be notCharIn or notCharRange)
const alphabet = charRange`a``z`;
const specialWord = charIn`0-9`(alphabet)(symbols);
const specialWord2 = charIn`0-9${alphabet}${symbols}`;
const notSpecialWord = notCharIn`0-9`(alphabet)(symbols);
```
