CKTIS / JavaScript
RegExp character classes, quantifiers and anchors
Choose what a pattern can match, how many times it can repeat, and where it can match. Combine these tools to validate an entire string or find a matching line.
Version: ECMAScript 2024 and the current web platform; runtime support varies
Syntax
Character classes: [...], [^...], [a-z], \d, \D, \w, \W, \s, \S
Unicode property escapes with u or v: \p{Letter}, \P{Letter}
Class syntax with v: [A&&B], [A--B], [\q{ab|cd}]
Wildcard: . (includes line terminators with s)
Quantifiers after an atom: *, +, ?, {n}, {n,}, {n,m}; append ? for lazy repetition
Anchors and boundary assertions: ^, $, \b, \B
Example pattern: /^[A-Z]{2}-\d{2}$/RegExp character classes, quantifiers and anchors answer three questions: what, how many, and where. [abc] matches one member of a set, [a-z] includes both endpoints of the range, and [^abc] matches one character outside the set. \d matches an ASCII digit, \s matches whitespace (including line terminators), and \w matches ASCII letters, ASCII digits, or underscore. With i and either u or v, \w also includes characters that Unicode case folding maps into that set. \D, \S, and \W are the respective complements. Outside brackets, . excludes line terminators unless s is set; [.] matches a literal dot.
Unicode. Without u or v, matching uses UTF-16 code units; with either flag, it uses code points, not grapheme clusters. \p{Letter} and \P{Letter} require u or v and match Unicode letters and nonletters, respectively. v adds class intersection (&&), subtraction (--), and finite sets of strings such as [\q{ab|cd}]. Do not combine u and v; check v support in your target runtime.
Quantifiers apply only to the preceding atom; use (?:ab)+ to repeat the whole sequence. * means zero or more, + one or more, ? zero or one, {n} exactly n, {n,} at least n, and {n,m} n through m inclusive. A minimum greater than the maximum causes a SyntaxError. Greedy quantifiers try more repetitions first; a trailing ?, as in +?, makes them lazy and reverses that preference. The rest of the pattern can force a different count.
Anchors and boundaries check a position without consuming characters. Without m, ^ and $ mean the start and end of the entire input; $ does not match before a trailing line terminator. With m, they also match immediately after and before a line terminator, respectively. \b checks that exactly one side is a \w character, with positions outside the input treated as non-word; \B checks the opposite. Inside a class, \b means backspace. To validate the whole input with alternatives, group them as in ^(?:yes|no)$ and leave m off.
Examples
const whole = /^[A-Z]{2}-\d{2}$/;
const line = /^[A-Z]{2}-\d{2}$/m;
console.log(whole.test("AB-42"));
console.log(whole.test("Ab-42"));
console.log(whole.test("AB-4"));
console.log(whole.test("AB-42\n"));
console.log(line.test("header\nAB-42\nfooter"));Result
true false false false true
`whole` requires two uppercase ASCII letters, a hyphen, and two ASCII digits. It rejects a lowercase letter, a missing digit, and a trailing line feed. With `m`, `line` finds the valid code on the middle line.
Related lessons
- JavaScript · Lesson 25 · Intermediate · Version: 3