Skip to content
Toolcroft

Developer Tools

Regex Cheatsheet & Tester

Searchable regex cheatsheet with examples for anchors, quantifiers, character classes, groups, lookaheads, and more. Test patterns against sample text.

Anchors

Start of string (or line with m flag)

^Hello matches "Hello World"

End of string (or line with m flag)

World$ matches "Hello World"

Word boundary

\bcat\b matches "cat" but not "scatter"

Non-word boundary

\Bcat\B matches "scatter"

Character Classes

Any character except newline

h.t matches "hat", "hit", "hot"

Digit [0-9]

\d+ matches "123"

Non-digit

\D+ matches "abc"

Word char [a-zA-Z0-9_]

\w+ matches "hello_123"

Non-word character

\W+ matches "!@#"

Whitespace (space, tab, newline)

\s+ matches spaces

Non-whitespace

\S+ matches words

Character set: a, b, or c

[aeiou] matches vowels

Negated set: not a, b, or c

[^aeiou] matches consonants

Range: lowercase a to z

[a-z]+ matches "hello"

Quantifiers

Zero or more (greedy)

a* matches "", "a", "aaa"

One or more (greedy)

a+ matches "a", "aaa"

Zero or one (optional)

colou?r matches "color" and "colour"

Exactly n times

\d{3} matches "123"

n or more times

\d{2,} matches "12", "123"

Between n and m times

\d{2,4} matches "12" to "1234"

Zero or more (lazy)

<.+?> lazily matches tags

One or more (lazy)

a+? matches minimal "a"

Groups

Capturing group

(foo)+ captures "foo"

Non-capturing group

(?:foo)+ groups without capture

Named capturing group

(?<year>\d{4}) named capture

Alternation (a or b)

cat|dog matches "cat" or "dog"

Lookarounds

Positive lookahead

\d(?=px) matches digit before "px"

Negative lookahead

\d(?!px) matches digit not before "px"

Positive lookbehind

(?<=\$)\d+ matches digits after $

Negative lookbehind

(?<!\$)\d+ matches digits not after $

Flags

Global - find all matches

/a/g finds all "a"

Case-insensitive

/hello/i matches "Hello"

Multiline - ^ and $ match line boundaries

/^start/m

Dot matches newline too

/.+/s spans multiple lines

Escaping

Escape special character

\. matches literal dot

Common Patterns

Email address

test@example.com

HTTP/HTTPS URL start

https://example.com

IPv4 address

192.168.0.1

Hex color code

#ff0000 or #f00

Regex Tester

3 matches

Phone: 555-1234, Zip: 90210

Regex quick reference

PatternMeaning
.Any character except newline
^Start of string
$End of string
*0 or more of the preceding element
+1 or more of the preceding element
?0 or 1 (makes preceding element optional)
{n,m}Between n and m occurrences
{n}Exactly n occurrences
[abc]Any one of a, b, or c
[^abc]Any character except a, b, or c
(a|b)a or b (alternation)
\dDigit [0-9]
\DNon-digit
\wWord character [a-zA-Z0-9_]
\WNon-word character
\sWhitespace (space, tab, newline)
\SNon-whitespace
\nNewline character
\tTab character
\bWord boundary
(?:...)Non-capturing group
(?=...)Lookahead (followed by)
(?!...)Negative lookahead (not followed by)
(?<=...)Lookbehind (preceded by)
(?<!...)Negative lookbehind

Flags

  • g - global: find all matches (not just first)
  • i - case-insensitive
  • m - multiline: ^ and $ match line boundaries
  • s - dotAll: . matches newlines too

Named capture groups

ES2018 introduced named capture groups using (?<name>...):

const re = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/;
const m = '2025-07-04'.match(re);
console.log(m.groups.year);  // "2025"
console.log(m.groups.month); // "07"

Named groups make patterns self-documenting and are accessible via m.groups. They also work in String.prototype.replace() using the $<name> syntax.

JavaScript-specific notes

  • matchAll(): returns an iterator of all matches including capture groups. Requires the g flag. Use it instead of looping with exec(): for (const m of str.matchAll(re)) { ... }
  • exec() loop pattern: when using a stateful regex (with g flag) and exec(), the regex updates its lastIndex after each call. Loop with while ((m = re.exec(str)) !== null) and beware of infinite loops if the regex can match an empty string.
  • Sticky flag y: matches only at lastIndex; does not scan forward. Useful for tokenizers.