Metacharacters
The single-character building blocks. Most regex patterns are made of these plus a few quantifiers and groups.
| Syntax | Meaning | Example |
|---|---|---|
| . | Any character except newline (unless s flag is set). | /a.c/ → matches abc, a c, a9c |
| \d | Any digit — equivalent to [0-9]. | /\d+/ → matches 42, 2026 |
| \D | Any non-digit — equivalent to [^0-9]. | /\D+/ → matches letters, spaces, symbols |
| \w | Any word character — equivalent to [A-Za-z0-9_]. | /\w+/ → matches hello_42 |
| \W | Any non-word character. | /\W/ → matches spaces, punctuation |
| \s | Any whitespace — space, tab, newline, etc. | /\s+/ → matches run of whitespace |
| \S | Any non-whitespace. | /\S+/ → matches runs of text without spaces |
| \b | Word boundary — zero-width assertion between \w and \W. | /\bword\b/ → matches word but not swords |
| \B | Non-word boundary. | /\Bing\b/ → matches ing inside a word |
| \\ | A literal backslash. | /C:\\Users/ → matches C:\Users |
Quantifiers
How many of the preceding token to match. Every quantifier has a greedy default (match as much as possible) and a lazy variant with a trailing ? (match as little as possible).
| Syntax | Meaning | Example |
|---|---|---|
| * | Zero or more of the preceding token — greedy. | /a*/ → matches '', a, aa, aaaa |
| + | One or more — greedy. | /a+/ → matches a, aa, aaaa (not empty) |
| ? | Zero or one — greedy. | /colou?r/ → matches color and colour |
| {n} | Exactly n of the preceding token. | /\d{3}/ → matches 123 but not 12 or 1234+ |
| {n,} | n or more. | /a{2,}/ → matches aa, aaa, aaaa |
| {n,m} | Between n and m inclusive — greedy. | /\d{2,4}/ → matches 12, 123, 1234 |
| *? | Zero or more — lazy (as few as possible). | /<.*?>/ → matches <a>, not <a>...<b> |
| +? | One or more — lazy. | /".+?"/ → matches each quoted string separately |
| ?? | Zero or one — lazy. | /a??/ → prefers to match empty |
Character classes
Match any character from a set. Negate with a leading ^. Use ranges for contiguous sets. Unicode property escapes require the u flag.
| Syntax | Meaning | Example |
|---|---|---|
| [abc] | Any one of a, b, or c. | /[aeiou]/ → matches any vowel |
| [^abc] | Any character except a, b, or c. | /[^0-9]/ → matches any non-digit |
| [a-z] | Any lowercase letter (range). | /[a-zA-Z]/ → any letter |
| [0-9] | Any digit (range). | /[0-9]{3}-[0-9]{4}/ → US phone |
| \p{L} | Unicode category — any letter. Requires u flag. | /\p{L}+/u → any Unicode word |
| \p{N} | Unicode category — any number. Requires u flag. | /\p{N}+/u → any Unicode digit |
Anchors
Zero-width assertions — they match positions, not characters. Useful for binding patterns to line starts, ends, or word boundaries.
| Syntax | Meaning | Example |
|---|---|---|
| ^ | Start of string (or start of line with m flag). | /^Hello/ → matches at the beginning |
| $ | End of string (or end of line with m flag). | /world$/ → matches at the end |
| \A | Start of string only — not supported in JS regex; use ^ without m flag. | — |
| \z | End of string only — not supported in JS regex; use $ without m flag. | — |
Groups and captures
Groups serve three purposes: applying quantifiers to multiple tokens, capturing a substring you can reference later, and structuring alternation.
| Syntax | Meaning | Example |
|---|---|---|
| (abc) | Capturing group — referenced as $1, $2, … | /(\w+)@(\w+)/ → $1 = user, $2 = domain |
| (?:abc) | Non-capturing group — grouping without capture. | /(?:ab)+/ → matches abab, no capture |
| (?<name>abc) | Named capture — referenced as $<name>. | /(?<year>\d{4})/ → .groups.year |
| \1 \2 | Backreference to a previous capture. | /(\w)\1/ → matches doubled letters |
| \k<name> | Named backreference. | /(?<q>['"]).*?\k<q>/ → balanced quotes |
Look-around
Zero-width assertions that check what's around the current position without consuming characters. JavaScript supports all four variants since ES2018.
| Syntax | Meaning | Example |
|---|---|---|
| (?=abc) | Positive lookahead — followed by abc (not consumed). | /foo(?=bar)/ → matches foo before bar |
| (?!abc) | Negative lookahead — not followed by abc. | /foo(?!bar)/ → matches foo NOT before bar |
| (?<=abc) | Positive lookbehind — preceded by abc. | /(?<=\$)\d+/ → digits after $ |
| (?<!abc) | Negative lookbehind — not preceded by abc. | /(?<!\$)\d+/ → digits NOT after $ |
Flags
Modify how the pattern matches. In JavaScript regex literal form, flags go after the closing /: /pattern/gi.
| Syntax | Meaning | Example |
|---|---|---|
| g | Global — find all matches, not just the first. | /\d/g on '123' matches 3 times |
| i | Ignore case. | /cat/i matches Cat, CAT, cAt |
| m | Multiline — ^ and $ match line boundaries. | /^end$/m on multi-line input |
| s | Dotall — the . metacharacter also matches newlines. | /start.*end/s spans lines |
| u | Unicode — enables \p{...} and proper surrogate handling. | /\p{Emoji}/u matches emoji |
| y | Sticky — each match must start exactly at lastIndex. | Used in parsers for tokenizing |
Replacement substitutions
In replacement strings (for .replace()or our tester's replace mode), these special tokens are substituted with captured values.
| Syntax | Meaning | Example |
|---|---|---|
| $& | The whole match. | 'hello'.replace(/./g, '$&_') → 'h_e_l_l_o_' |
| $1, $2, … | Numbered capture groups. | 'John Smith' → /(\w+) (\w+)/ → '$2, $1' = 'Smith, John' |
| $<name> | Named capture group. | /(?<year>\d{4})/ → '$<year>' |
| $` | The portion before the match. | Prefix before the matched substring. |
| $' | The portion after the match. | Suffix after the matched substring. |
| $$ | A literal dollar sign. | 'USD'.replace(/USD/, '$$') → '$' |
Common patterns
Production-grade regexes for the tasks everyone writes ten times. Copy-paste into the tester to see each one match.
· US phone number (loose)
^(?:\(\d{3}\)|\d{3})[- ]?\d{3}[- ]?\d{4}$Matches 5551234567, 555-123-4567, (555) 123-4567. Doesn't validate area code.
· Email (loose — good enough for UX validation)
^[^\s@]+@[^\s@]+\.[^\s@]+$
Catches obvious typos. For production, send a verification email — the RFC-compliant regex is ~300 chars.
· URL
^https?:\/\/[^\s/$.?#].[^\s]*$
Matches http:// or https:// followed by a hostname + path.
· ISO date (YYYY-MM-DD)
^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$Validates shape; doesn't catch Feb 30 or Apr 31.
· Hex color
^#(?:[0-9a-fA-F]{3}){1,2}$Matches #abc and #aabbcc; not 4/8-digit alpha variants.
· IPv4 address (strict)
^(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)$Validates each octet is 0-255.
· Whitespace (collapse runs to one space)
\s+
Replace with ' ' to normalize whitespace.
· Leading/trailing whitespace (trim)
^\s+|\s+$
Replace with '' for a regex-based trim.
Related
- · Live regex tester — paste any pattern above into it to see the matches highlighted.
- · Python regex cheat sheet — the re module, methods, and the small syntax differences from JavaScript.
- · UUID generator — the other dev tool on Briskly.