How to test a regular expression
Type a pattern, add flags, and paste text to search. Matches are highlighted as you type, the match count updates, and capture groups are listed for each match. Use Prev and Next, or the [ and ] keys, to step through matches in long text. Patterns run on your browser's JavaScript regex engine, so results match what new RegExp() does in your code.
Cheat sheet
.any character except a newline ·\ddigit ·\wletter, digit or underscore ·\swhitespace[abc]one of a, b or c ·[^abc]anything else ·[a-z]a range^start ·$end (of each line with the m flag) ·\bword boundary*0 or more ·+1 or more ·?0 or 1 ·{2,4}between 2 and 4(...)capture group ·(?:...)group without capturing ·(?<name>...)named group ·a|beither- Flags:
gfind all matches,iignore case,mmultiline,slet.match newlines,uUnicode
Greedy vs lazy
Quantifiers are greedy: they match as much as they can. On the text <a><b>, the pattern <.+> matches the whole string. Add ? to make it lazy: <.+?> matches <a> and then <b>.
Patterns people search for
Simple email check: ^[^\s@]+@[^\s@]+\.[^\s@]+$
ISO date: ^\d{4}-\d{2}-\d{2}$
Hex colour: ^#(?:[0-9a-fA-F]{3}){1,2}$
Whole word "error": \berror\bThe email pattern catches obvious typos but doesn't follow the full email standard; the only real check is sending a confirmation email.
Avoid catastrophic backtracking
Nested quantifiers like (a+)+$ can take exponential time on text that almost matches, freezing a browser tab or a server. Prefer specific character classes over .*, and avoid repeating a group that itself repeats.