Regex Memo

Regex syntax quick reference and common patterns, covering character classes, quantifiers, anchors, assertions, etc。 Runs locally—nothing uploaded.

Character Classes

[abc]Any of a, b, or c/[aeiou]/.test('a') → true
[^abc]Not a, b, nor c/[^aeiou]/.test('b') → true
[a-z]Character range a to z/[a-z]/.test('m') → true
.Any character except newline/./.test('x') → true
\dDigit [0-9]/\d/.test('5') → true
\DNon-digit [^0-9]/\D/.test('a') → true
\wWord character [a-zA-Z0-9_]/\w/.test('_') → true
\WNon-word character/\W/.test('!') → true
\sWhitespace/\s/.test(' ') → true
\SNon-whitespace/\S/.test('a') → true

Quantifiers

*0 or more/go*/.test('g') → true
+1 or more/go+/.test('go') → true
?0 or 1 (optional)/colou?r/.test('color') → true
{n}Exactly n/a{3}/.test('aaa') → true
{n,}n or more/a{2,}/.test('aaa') → true
{n,m}Between n and m/a{2,4}/.test('aaa') → true
*?0 or more (lazy)/a*?/.exec('aaa') → ''
+?1 or more (lazy)/a+?/.exec('aaa') → 'a'

Anchors

^Start of string/^hello/.test('hello world') → true
$End of string/world$/.test('hello world') → true
\bWord boundary/\bword\b/.test('a word b') → true
\BNon-word boundary/\Bord\B/.test('word') → false

Groups & Capturing

(abc)Capture group/(ab)c/.exec('abc')[1] → 'ab'
(?:abc)Non-capturing group/(?:ab)c/.exec('abc')[1] → undefined
(?<name>abc)Named capture group/(?<w>ab)c/.exec('abc').groups.w → 'ab'
\1Backreference to group 1/(a)\1/.test('aa') → true
$1Replacement reference'ab'.replace(/(a)/, '$1x') → 'axb'

Lookaround Assertions

(?=abc)Positive lookahead/a(?=b)/.test('ab') → true
(?!abc)Negative lookahead/a(?!b)/.test('ac') → true
(?<=abc)Positive lookbehind/(?<=a)b/.test('ab') → true
(?<!abc)Negative lookbehind/(?<!a)b/.test('cb') → true

Flags

gGlobal — find all matches'aaa'.replace(/a/g, 'b') → 'bbb'
iCase-insensitive/a/i.test('A') → true
mMultiline (^/$ match line ends)/^b/m.test('a\nb') → true
sDotall (. matches newline)/a.b/s.test('a\nb') → true
uUnicode/\u{1F600}/u.test('😀') → true
ySticky (match at lastIndex)/a/y.exec('aaa') → 'a' at 0

Common Patterns

^[\w.-]+@[\w.-]+\.\w+$Email (basic)/^[\w.-]+@[\w.-]+\.\w+$/.test('a@b.com') → true
^https?://[\S]+$URL (basic)/^https?:\/\/[\S]+$/.test('http://x') → true
^\d{4}-\d{2}-\d{2}$Date YYYY-MM-DD/^\d{4}-\d{2}-\d{2}$/.test('2024-01-01') → true
^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$Hex color/^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/.test('#fff') → true
^\d+$Integer only/^\d+$/.test('123') → true
^-?\d+(\.\d+)?$Decimal number/^-?\d+(\.\d+)?$/.test('-3.14') → true
^(?=.*[A-Z])(?=.*[a-z])(?=.*\d).{8,}$Strong password (8+ chars, upper, lower, digit)test('Abc12345') → true