Character Classes
word
\w
Match alphanumeric characters and underscore
not.word
not(word)
\W
Match anything other than \w
digit
\d
Match a character between 0 to 9
not.digit
not(digit)
\D
Match anything other than a digit
whitespace
\s
Match all types of whitespace characters
not.whitespace
not(whitespace)
\S
Match anything other than a whitespace
char
.
Match any character other than newline (or including line terminators when single line flag is set)
Note: not.char does not exist because it does not match anything
charIn`a-z_-`
charIn('a-z', '_-')
charIn`a-z` `_-`
[a-z_-]
Match a character listed in the group. A hyphen denotes a range of characters, such as a-z. †
notCharIn`a-z_-`
not.charIn`a-z_-`
notCharIn('a-z', '_-')
notCharIn`a-z` `_-`
[^a-z_-]
Match a character not listed in the group. †
charRange`a` `z`
charRange('a', 'z')
[a-z]
Match a character ranging from the first char to the second one. Escape sequences are supported and the two characters must be in order.
notCharRange`a` `z`
not.charRange`a` `z`
notCharRange('a', 'z')
[^a-z]
Match a character not in the range of first to second char. Escape sequences are supported and the two characters must be in order.
† 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:
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);Last updated