Regex Cheat Sheet
Anchors
| Token | Description |
|---|---|
^ | Start of string, or start of line with the m flag |
$ | End of string, or end of line with the m flag |
\b | Word boundary |
\B | Not a word boundary |
Quantifiers
| Token | Description |
|---|---|
* | Zero or more times |
+ | One or more times |
? | Zero or one time |
{n} | Exactly n times |
{n,} | At least n times |
{n,m} | Between n and m times |
Character Classes
| Token | Description |
|---|---|
. | Any character except line terminators |
\d | Any digit character (0-9) |
\D | Any non-digit character |
\w | Any word character (letter, digit, underscore) |
\W | Any non-word character |
\s | Any whitespace character |
\S | Any non-whitespace character |
[abc] | Any one of the characters a, b, or c |
[^abc] | Any character except a, b, or c |
[a-z] | Any character in the range a to z |
Groups & Alternation
| Token | Description |
|---|---|
(abc) | Capturing group |
(?:abc) | Non-capturing group |
(?<name>abc) | Named capturing group |
\1 | Backreference to capture group 1 |
| | Alternation (OR) |
Flags
| Token | Description |
|---|---|
g | Global — find all matches instead of stopping after the first |
i | Case-insensitive matching |
m | Multiline (JS) / Dotall (Ruby/Python) — ^ and $ match line boundaries or . matches newlines, depending on flavor |
s | Dotall — . also matches newlines |
u | Unicode — enables full unicode matching |
x | Extended (Ruby/Python) — ignore whitespace and allow # comments in the pattern |
y | Sticky — matches only from lastIndex |