JavaScript regular expressions quick reference — character classes, quantifiers, groups, flags, assertions
A concise reference for JavaScript (RegExp) regular expressions. All syntax is tested against the V8 engine running in your browser.
| Flag | Name | Effect |
|---|---|---|
| g | Global | Find all matches, not just the first |
| i | Case-insensitive | Match uppercase and lowercase equally |
| m | Multiline | ^ and $ match start/end of each line |
| s | DotAll | . matches newline characters too |
| u | Unicode | Enable full Unicode support and strict escaping |
| d | Indices | Add indices array with start/end positions of each group |
| Syntax | Matches |
|---|---|
| . | Any character except newline (use s flag to include \n) |
| \d | Digit [0-9] |
| \D | Non-digit |
| \w | Word character [a-zA-Z0-9_] |
| \W | Non-word character |
| \s | Whitespace (space, tab, newline, carriage return…) |
| \S | Non-whitespace |
| [abc] | Any of a, b, c |
| [^abc] | Any character except a, b, c |
| [a-z] | Any lowercase letter |
| [a-zA-Z0-9] | Letter or digit |
| Syntax | Matches |
|---|---|
| ^ | Start of string (or line with m flag) |
| $ | End of string (or line with m flag) |
| \b | Word boundary |
| \B | Non-word boundary |
| Syntax | Meaning |
|---|---|
| * | 0 or more (greedy) |
| + | 1 or more (greedy) |
| ? | 0 or 1 (optional) |
| {n} | Exactly n times |
| {n,} | At least n times |
| {n,m} | Between n and m times |
| *? +? ?? | Lazy (non-greedy) versions — match as few chars as possible |
| Syntax | Meaning |
|---|---|
| (abc) | Capturing group — accessible as $1, match[1] |
| (?:abc) | Non-capturing group — groups without capturing |
| (?<name>abc) | Named capture group — accessible as match.groups.name |
| (?=abc) | Positive lookahead — position followed by "abc" |
| (?!abc) | Negative lookahead — position NOT followed by "abc" |
| (?<=abc) | Positive lookbehind — position preceded by "abc" |
| (?<!abc) | Negative lookbehind — position NOT preceded by "abc" |
| a|b | Alternation — matches a or b |
| Use case | Pattern |
|---|---|
| [a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,} | |
| URL | https?://[^\s/$.?#].[^\s]* |
| ISO date | \d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01]) |
| IPv4 | \b(?:\d{1,3}\.){3}\d{1,3}\b |
| Hex color | #(?:[0-9a-fA-F]{3}){1,2}\b |
| Semantic version | \d+\.\d+\.\d+(?:-[a-zA-Z0-9.]+)? |