Characters
| Token | Meaning |
|---|
. | Any character except newline |
\d | Digit 0–9 |
\D | Not a digit |
\w | Word character [A-Za-z0-9_] |
\W | Not a word character |
\s | Whitespace (space, tab, newline…) |
\S | Not whitespace |
\t \n \r | Tab, newline, carriage return |
\uFFFF | Unicode code unit |
\p{L} | Any letter (needs u flag) |
Sets
| Token | Meaning |
|---|
[abc] | a, b or c |
[^abc] | Anything but a, b, c |
[a-z] | Range a to z |
[a-zA-Z0-9] | Alphanumeric |
Anchors
| Token | Meaning |
|---|
^ | Start of string (line with m) |
$ | End of string (line with m) |
\b | Word boundary |
\B | Not a word boundary |
Quantifiers
| Token | Meaning |
|---|
* | 0 or more |
+ | 1 or more |
? | 0 or 1 |
{3} | Exactly 3 |
{2,} | 2 or more |
{2,5} | 2 to 5 |
*? +? ?? | Lazy (as few as possible) |
Groups
| Token | Meaning |
|---|
(abc) | Capture group |
(?:abc) | Non-capturing group |
(?<name>abc) | Named group |
\1 | Back-reference to group 1 |
\k<name> | Back-reference by name |
a|b | a or b |
Lookaround
| Token | Meaning |
|---|
(?=x) | Followed by x |
(?!x) | Not followed by x |
(?<=x) | Preceded by x |
(?<!x) | Not preceded by x |
Replacement
| Token | Meaning |
|---|
$1 | Group 1 |
$<name> | Named group |
$& | Whole match |
$` | Text before match |
$' | Text after match |
$$ | Literal $ |
Worked example
To capture the year, month and day from 2026-11-26 use (\d{4})-(\d{2})-(\d{2}). Replacing with $3/$2/$1 produces 26/11/2026.