Character Classes
Character classes are a set of characters that match any one of the characters in the set.
Common character class escapes
const any: RegexConstruct;
const word: CharacterEscape;
const nonWord: CharacterEscape;
const digit: CharacterEscape;
const nonDigit: CharacterEscape;
const whitespace: CharacterEscape;
const nonWhitespace: CharacterEscape;
any
matches any character except newline characters. Regex syntax:.
.word
matches any word character (letters, digits & underscore). Regex syntax:\w
.nonWord
matches any character except word characters (letters, digits & underscore). Regex syntax:\W
.digit
matches any digit. Regex syntax:\d
.nonDigit
matches any character except digits. Regex syntax:\D
.whitespace
matches any whitespace character (spaces, tabs, line breaks). Regex syntax:\s
.nonWhitespace
matches any character except whitespace characters (spaces, tabs, line breaks). Regex syntax:\S
.
anyOf()
function anyOf(characters: string): CharacterClass;
Regex syntax: [abc]
.
The anyOf
class matches any character in the character
string.
Example: anyOf('aeiou')
will match either a
, e
, i
o
or u
characters.
charRange()
function charRange(start: string, end: string): CharacterClass;
Regex syntax: [a-z]
.
The charRange
class matches any characters in the range from start
to end
(inclusive).
Examples:
charRange('a', 'z')
will match all lowercase characters froma
toz
.charRange('A', 'Z')
will match all uppercase characters fromA
toZ
.charRange('0', '9')
will match all digit characters from0
to9
.
charClass()
function charClass(...elements: CharacterClass[]): CharacterClass;
Regex syntax: [...]
.
The charClass
construct creates a new character class that includes all passed character classes.
Examples:
charClass(charRange('a', 'f'), digit)
will match all lowercase hex digits (0
to9
anda
tof
).charClass(charRange('a', 'z'), digit, anyOf("._-"))
will match any digit, lowercase Latin letter froma
toz
, and either of.
,_
, and-
characters.
negated()
function negated(element: CharacterClass): RegexConstruct;
Regex syntax: [^...]
.
The negated
construct creates a new character class that matches any character not present in the passed character class.
Examples:
negated(digit)
matches any character that is not a digitnegated(anyOf('aeiou'))
matches any character that is not a lowercase vowel.