About JavaScript regular expressions
What this tester runs
This page tests JavaScript regular expressions: the RegExp engine built into your browser and Node.js. The pattern is compiled with new RegExp(pattern, flags), so a pattern that works here behaves the same in front-end code, a Node script or a replace() call.
Results come in four views, each with its own URL. Matches lists every match with its position and capture groups. Replace previews text.replace(). List puts each match, or one chosen group, on its own line, optionally without duplicates. Split shows text.split(), including any pieces caught by capture groups, as the real call returns them.
The pattern runs in a Web Worker. If it runs for more than one second it is stopped, so a runaway pattern cannot freeze the tab. The first 1,000 matches are listed. Your pattern and text are kept in this browser's local storage and are never uploaded.
The flags, and why Unicode matters
JavaScript has eight flags, and each is a toggle here. g finds every match instead of the first. i ignores case. m makes ^ and $ match at every line break, not only at the start and end of the text. s lets . match line breaks too. u reads the pattern and text as Unicode code points and enables \p{…} property classes. v builds on u with set operations inside classes, such as [\p{L}--[a-z]], and cannot be combined with it. y (sticky) matches only at the current position. d records where each capture group starts and ends.
Without u, an emoji is two UTF-16 code units, so . matches half of it. Even with u, \d and \w stay ASCII-only: \w+ stops at the é in “café”. For letters and digits in any script, use \p{L} and \p{Nd} with the u flag.
Coming from PCRE, Python or another flavour
Most syntax carries over: classes, quantifiers, lazy quantifiers, lookahead, lookbehind, named groups and backreferences. The differences that catch people out are small but strict. Named groups are written (?<name>…), not Python's (?P<name>…). In a replacement, groups are $1 and $<name>, not \1; $& is the whole match and $$ is a literal dollar sign.
Possessive quantifiers (a++), atomic groups (?>…), recursion and a leading (?i) are not JavaScript syntax; use the i flag instead of (?i). There is no \A or \z: use ^ and $ with m off. Beware that without u, an unknown escape such as \A quietly matches the letter A instead of raising an error.
Backslashes are typed once here. In source code, a pattern passed as a string to new RegExp() needs each backslash doubled ("\\d+"), while a literal /\d+/ does not. The tool shows a ready-to-copy literal under the pattern box.
Catastrophic backtracking
Browser regex engines backtrack: when part of a pattern fails, they go back and try every other way the earlier parts could have matched. Usually that takes microseconds. With nested quantifiers over the same characters, the number of ways explodes. (a+)+$ tested against thirty as and then a b must try hundreds of millions of ways to split the run before it can report no match, and each extra a roughly doubles the work. On a server this is a denial-of-service bug known as ReDoS.
The fix is to remove the ambiguity so each character can be matched only one way. (a+)+$ means the same as a+$. (\w+\s?)+ can split a word many ways because the space is optional, while \w+(?:\s+\w+)* needs a space between repeats. Inside quotes, "[^"]*" is safer than ".*".
Frequently asked questions
- Why does my regex only find the first match?
- The g (global) flag is off. Without it, the engine stops after the first match. In code, str.match(/x/) returns the first match with its groups, str.match(/x/g) returns every match but no groups, and str.matchAll(/x/g) gives you both.
- Why does my regex work here but not in Python, Java or grep?
- Each language has its own flavour. Python names groups with (?P<name>…), Java strings need doubled backslashes, and grep defaults to POSIX basic syntax, where \d is not supported and + is a literal unless you use grep -E (or grep -P for Perl syntax). Test in the flavour you will run.
- How do I match a dot, bracket or other special character literally?
- Put a backslash before it. Outside a class, . * + ? ^ $ { } ( ) | [ ] and \ are special, so \. matches a dot and \( a bracket. Inside [ ], only ] \ ^ and - need care (the v flag adds a few more). A / does not need escaping in the pattern box, but it does in a /…/ literal; the copied literal escapes it for you.