Regex tester
JavaScript regular expressions with live matches and groups.
Matches
Cheat sheet
- .
- any char
- \d \w \s
- digit, word, space
- \b
- word boundary
- ^ $
- start, end
- * + ?
- 0+, 1+, 0 or 1
- {n,m}
- n to m times
- [abc] [^abc]
- set, negated set
- (a|b)
- group, alternation
- (?<name>…)
- named group
- (?:…)
- non-capturing
- (?=…) (?!…)
- lookahead
- (?<=…) (?<!…)
- lookbehind
- \1 $1
- backreference, replace
How to test a regular expression
Write the pattern
Type it between the slashes, without delimiters. Toggle flags with the buttons: g finds every match, i ignores case, m makes ^ and $ work per line, s lets . match newlines. Or pick one from Common patterns to start from.
Add test text
Paste into Test string, press Open to load a text file, or use Sample. Matches are highlighted as you type and listed in the table with their position and capture groups.
Replace if needed
Press Replace to show the replacement field. Use $1 for numbered groups, lt;name> for named groups and amp; for the whole match. The result appears in its own pane with a Copy button.
Good to know
- Patterns use JavaScript syntax: named groups (?<year>\d{4}), lookbehind (?<=...) and Unicode property classes with the u flag. PCRE-only features such as possessive quantifiers and atomic groups are not available.
- The g flag is always applied when listing matches, so you see them all. Replace honours your flags, so leave g off to replace only the first occurrence.
- A pattern that never finishes, usually nested quantifiers such as (a+)+ on a long string, is stopped after 3 seconds and the worker is restarted. Tighten the pattern rather than waiting.
- Ctrl+C (Cmd+C on Mac) with nothing selected copies every match, one per line, or the replaced text when Replace is open.
- Text files up to 512 KB load into the test string. Highlighting is skipped above 200,000 characters, the count includes every match, details are kept for the first 5,000 and the table lists the first 200.
- Matching runs in a web worker in your browser. Nothing is uploaded. The Cheat sheet at the bottom lists the tokens people forget.
Questions people ask
Why does Replace only change the first occurrence?
The g flag is off. The match list always shows every match, but Replace follows your flags exactly, the same as String.replace in JavaScript. Turn on g to replace them all.
How do I use capture groups in the replacement?
Wrap the part you need in parentheses and refer to it as $1, $2 and so on, or name it with (?<name>...) and use lt;name>. amp; inserts the whole match. The Groups column in the match table shows what each group captured.
Why does ^ or $ not match each line?
By default they match only the start and end of the whole text. Turn on the m flag so they match at every line break. For . to cross line breaks as well, turn on s.
Why do I get a timeout?
Some patterns backtrack exponentially on inputs that almost match, for example (\w+\s?)+$ on a long line. The tester stops them after 3 seconds. Make the pattern more specific, use non-greedy quantifiers or anchor it.
Will the pattern work in Python, Java or Go?
Mostly. The core syntax is shared, but details differ: named groups are (?P<name>) in Python, Go's RE2 has no lookaround or backreferences, and flag letters vary. Test in the target language before shipping.