No Login Data Private Local Save

JavaScript Regex Cheatsheet - Online Interactive Pattern Reference

6
0
0
0

JavaScript Regex Cheatsheet & Interactive Tester

Regex Tester
/ /
0 matches
Capture Groups
Match # Full Match Group 1 Group 2 Group 3 ...
Cheatsheet
Click to expand

^Start of string/line
$End of string/line
\bWord boundary
\BNon-word boundary

\dDigit [0-9]
\DNon-digit
\wWord character [A-Za-z0-9_]
\WNon-word character
\sWhitespace (space, tab, newline)
\SNon-whitespace
.Any character except newline

*0 or more times
+1 or more times
?0 or 1 time
{n}Exactly n times
{n,}At least n times
{n,m}Between n and m times
*? +? ??Lazy versions

(x)Capturing group
(?:x)Non-capturing group
x(?=y)Positive lookahead
x(?!y)Negative lookahead
(?<=y)xPositive lookbehind (ES2018)
(?Negative lookbehind (ES2018)
(?<name>x)Named capturing group (ES2018)

gGlobal search
iCase-insensitive
mMulti-line mode (^ and $ match line boundaries)
uUnicode mode
sDotall mode ('.' matches newline)
ySticky mode (matches from lastIndex)

regex.test(str)Returns true/false
regex.exec(str)Returns match details
str.match(regex)Returns matches (or first match without g)
str.matchAll(regex)Iterator of all matches (needs g flag)
str.replace(regex, newSubstr|fn)Replace matches
str.search(regex)Returns index of first match
str.split(regex)Split string by regex
Common Patterns

Click a pattern to use it in the tester.

Digits Words Letters Whole words Trim Email URL SSN Vowels Uppercase
FAQ

match() is a string method; without the g flag it returns the first full match and groups, with g it returns an array of all full matches (no groups). exec() is a regex method that returns one match at a time with groups and updates lastIndex (useful for looping). Use matchAll for all matches with groups.

The dot . matches any character except newline by default. To include newlines, enable the s (dotAll) flag: /pattern/s.

Use the i flag: /pattern/i.

A group (...) captures the matched substring for later use (e.g., in replacements or via exec). Non-capturing groups (?:...) group without capturing.

Use string.matchAll(regex) with the g flag. It returns an iterator of match arrays including groups. Alternatively, loop with exec() while regex.lastIndex is valid.

Use a function to escape: str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&').