Regex Cheatsheet — NextUtils
Complete regular expression reference with match examples and copy buttons.
Complete regular expression reference with match examples and copy buttons.
Discover our collection of free online tools for developers, designers, and power users
Advertisement
Complete syntax reference for regular expressions — anchors, character classes, quantifiers, groups, lookaheads, flags, and common real-world patterns. Every row includes match examples and a copy button. Test any pattern live in our Regex Tester.
Anchors assert a position in the string rather than consuming characters. They match where something is, not what it is.
^hello
Matches "hello" only at the very start of the string
world$
Matches "world" only at the very end of the string
^hello$
Match entire string — no leading or trailing characters allowed
\bcat\b
Match "cat" as a whole word only
\Bcat\B
Match "cat" only when inside a larger word
Match a single character from a defined set, range, or built-in shorthand class.
\d
Any digit: 0–9. Equivalent to [0-9]
\D
Any character that is NOT a digit
\w
Letters, digits, underscore: [a-zA-Z0-9_]
\W
Any character NOT in [a-zA-Z0-9_]
\s
Space, tab (\t), newline (\n), carriage return (\r)
\S
Any character that is NOT whitespace
.
Any single character except newline (\n)
[aeiou]
Match any single character listed in the brackets
[^0-9]
Match any character NOT in the set
[a-z]
Match any lowercase letter a through z
[a-zA-Z0-9]
Match any alphanumeric character
Quantifiers specify how many times the preceding element must match. All are greedy by default — they match as many characters as possible.
ab*c
"b" repeated zero or more times
ab+c
"b" must appear at least once
colou?r
"u" is optional — matches both spellings
a{3}Exactly 3 consecutive "a" characters
Use ^a{3}$ to enforce exact full-string match
a{2,}2 or more consecutive "a" characters
a{2,4}2 to 4 consecutive "a" characters (greedy — takes max)
<.+?>
Match fewest characters between < and > — stops at first >
Without ? the greedy .+ would match from first < to last >
Groups capture matched substrings for reuse. Use capturing groups for backreferences, non-capturing for grouping only.
(foo)bar
Captures "foo" in group 1 — accessible as match[1]
(?:foo)bar
Groups without creating a capture — more efficient
(?<year>\d{4})-(?<month>\d{2})Named capture — access via match.groups.year in JS
(\w+)\s\1
\1 repeats what group 1 captured — match duplicate words
cat|dog
Match either "cat" OR "dog"
gr(a|e)y
Group alternation to match both spellings
Lookarounds assert what comes before or after a position without consuming characters. The lookaround itself is not included in the match.
\d+(?= dollars)
Match digits only when followed by " dollars"
" dollars" is asserted but not included in the match result
\d+(?! dollars)
Match digits NOT followed by " dollars"
(?<=\$)\d+
Match digits only when preceded by "$"
(?<!\$)\d+
Match digits NOT preceded by "$"
Advertisement
Flags change how the entire pattern behaves. In JavaScript, they follow the closing slash: /pattern/flags.
/pattern/g
Find ALL matches, not just the first one
/hello/i
Match regardless of uppercase or lowercase
/^\w+/m
^ and $ match start/end of each LINE, not whole string
/a.b/s
Makes . match newline characters too
/\p{L}+/uEnable Unicode property escapes like \p{L}, \p{Emoji}
Required for \p{} Unicode property escapes
/pattern/y
Match only at the current lastIndex position
Unlike /g, sticky fails if match is not at exact lastIndex
Special escape sequences represent non-printable or hard-to-type characters inside a regex pattern.
\n
Matches a newline character (LF, U+000A)
\t
Matches a horizontal tab character (U+0009)
\r
Matches CR (U+000D) — present in Windows \r\n line endings
\\
Matches a single literal backslash character
\x41
Character with hex code — \x41 = "A" (0x41 = 65)
\u0041
Unicode code point — \u0041 = "A" (U+0041)
\u{1F600}Extended code point syntax — requires the /u flag
Must use /u flag: /\u{1F600}/u
These 12 characters have special meaning in regex. Prefix with \ to match them literally.
\.
Matches a literal period — unescaped . matches any char
\*
Matches a literal asterisk character
\+
Matches a literal plus sign
\?
Matches a literal question mark
\$
Matches a literal $ — unescaped $ is end-of-string anchor
\^
Matches a literal ^ — unescaped ^ is start-of-string anchor
\|
Matches a literal | — unescaped | means OR/alternation
\(note\)
Match literal parentheses — unescaped () create groups
\ ^ $ . * + ? ( ) [ ] { } |These 14 characters must be escaped to match literally
Inside a character class [...] most meta chars lose special meaning
Real-world regex patterns for common validation tasks. Copy and adapt for your project.
^[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}$Practical email validation (not full RFC 5322)
^https?:\/\/[^\s/$.?#].[^\s]*$
Match http:// or https:// URLs
Use /i flag for case-insensitive protocol matching
^\+?1?\s?\(?\d{3}\)?[\s.\-]?\d{3}[\s.\-]?\d{4}$Flexible US phone — handles many common formats
^(\d{1,3}\.){3}\d{1,3}$Basic IPv4 — four dot-separated octets
Strict version: ^(25[0-5]|2[0-4]\d|[01]?\d\d?)(\.(25[0-5]|2[0-4]\d|[01]?\d\d?)){3}$
^#([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$CSS hex color — 3 or 6 hex digits with # prefix
^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$ISO 8601 date — validates month (01–12) and day (01–31)
^[a-z0-9]+(?:-[a-z0-9]+)*$
Lowercase letters, digits, hyphens — no leading/trailing hyphen
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$Min 8 chars with uppercase, lowercase, digit, and special char
Uses four positive lookaheads to enforce all requirements simultaneously
<([a-z][a-z0-9]*)\b[^>]*>(.*?)<\/\1>
Match opening + content + matching closing tag via backreference
Uses \1 backreference to require matching tag names
How to create and use regular expressions in JavaScript — RegExp literals, constructors, and String/RegExp methods.
/pattern/flags
Create a regex inline — evaluated at parse time
new RegExp('pattern', 'flags')Create regex from a string — useful for dynamic patterns
Backslashes must be doubled in RegExp string: "\\d" not "\d"
/pattern/.test(str)
Returns true/false — fastest way to check if pattern matches
str.match(/pattern/g)
Returns array of matches, or null if none found
[...str.matchAll(/pattern/g)]
Returns iterator of all match objects including groups
Requires /g flag. Each result includes index, input, groups
str.replace(/pattern/g, replacement)
Replace all matches with a string or function
str.split(/pattern/)
Split string on a regex delimiter
str.match(/(?<year>\d{4})-(?<month>\d{2})/)Access captured groups by name via match.groups
Supported in all modern browsers. Use /u flag for full Unicode support
The * (star) quantifier matches zero or more occurrences — the element is optional. The + (plus) quantifier matches one or more — the element must appear at least once. /ab*c/ matches "ac", "abc", "abbc". /ab+c/ only matches "abc", "abbc" — not "ac".
Add the i flag: /pattern/i. For example, /hello/i matches "Hello", "HELLO", "HeLLo". With RegExp constructor: new RegExp("hello", "i"). Combine flags: /pattern/gi for global case-insensitive.
Capturing group () saves the matched text — accessible as match[1] or $1 in replacements. Non-capturing group (?:) groups elements for quantifiers or alternation without storing the match. Use (?<name>pattern) for named captures in JavaScript.
\b matches the position between a word character and a non-word character (or string start/end). It is zero-width — it does not consume characters. /\bcat\b/ matches "cat" in "the cat sat" but not in "cats" or "concatenate". Use \B for the opposite.
Greedy quantifiers (*, +, ?) match as many characters as possible. Lazy quantifiers (*?, +?, ??) match as few as possible. On "<b>text</b><i>more</i>", /<.+>/ matches the whole string, while /<.+?>/ matches just "<b>" then "</b>" etc. Add ? after any quantifier to make it lazy.
Advertisement