- Writing the pattern
- Enter the pattern without surrounding slashes, \d{3}-\d{4} rather than /\d{3}-\d{4}/. Flags are set with the checkboxes rather than written after a closing slash.
- What each flag does
- Global (g) finds every match instead of stopping at the first. Ignore case (i) is self-explanatory. Multiline (m) makes ^ and $ match at line boundaries rather than only at the start and end of the whole string. Dot-all (s) lets . match a newline, which it otherwise never does. Unicode (u) enables \p{...} property escapes and makes the pattern operate on code points, so an emoji counts as one character rather than two.
- Capture groups, numbered and named
- Each parenthesised group is reported by number in order of its opening bracket, and named groups written (?<name>...) are reported by name as well. A group that took part in no match shows explicitly as "(no match)", which is a common cause of an unexpected undefined in code, distinguishing "matched an empty string" from "did not participate" is often the bug.
- Patterns that could hang are rejected
- A pattern like (a+)+ against text that nearly matches can take exponential time, and JavaScript regex execution cannot be interrupted once started. It would freeze the page outright. Patterns with a quantifier applied to an already-quantified group, or a repeated alternation with overlapping branches, are therefore refused with an explanation. Nested quantifiers are almost always unintentional: (a+)+ matches exactly what a+ matches.
- Common pitfalls worth knowing
- Inside a character class most metacharacters lose their meaning, so [.] matches a literal dot. The dot never matches a newline unless the s flag is set. Quantifiers are greedy by default, so .* takes as much as possible, add ? to make it lazy. And \b depends on the definition of a word character, which is ASCII-only without the u flag.