About this tool
A regular expression (regex) is a pattern that describes how to match text, widely used for searching, validating and bulk-replacing strings. It combines literal characters with metacharacters: literals stand for themselves, while metacharacters such as \d, \w, ., *, +, ?, (), [] express quantities, positions or groups.
This tool uses the regular expression engine built into your browser, so every match is computed locally and your pattern or text is never sent to any server. As you type, each match is highlighted in real time, with its position and capture groups listed below.
Keep in mind that regex engines differ slightly across languages and platforms (for example in named-group syntax or some escaping rules). This tool follows JavaScript regex syntax, so the expressions you build can be used directly in JS or anywhere that follows the same syntax.
Regular expressions are built from a small set of parts that combine predictably. Literal characters match themselves; character classes such as \d, \w and [a-z] match one character from a set; anchors such as ^ and $ match positions rather than characters; quantifiers (*, +, ?, {2,5}) repeat the previous element; parentheses group and capture; and the pipe | expresses alternatives. Lookahead and lookbehind add zero-width conditions, which is how you assert that something is followed or preceded by a pattern without consuming it.
The most common real-world problem with regex is not syntax but performance. A pattern such as (a+)+b can backtrack exponentially on a string of many a characters followed by anything other than b, freezing the browser tab — the so-called catastrophic backtracking or ReDoS. Defensive habits help: anchor patterns when the whole string must match, prefer specific character classes over ., replace nested quantifiers with a single bounded one, and test the expression against long, deliberately bad input before shipping it to a server.
Regular expression engines are not identical. JavaScript follows ECMAScript syntax; PCRE (PHP, Perl), Python and Java differ in named groups, lookbehind support, possessive quantifiers and Unicode handling. JavaScript supports Unicode property escapes such as \p{Script=Han} when the u flag is set, which is far more reliable than hand-written ranges for non-Latin scripts. Named groups use (?<name>...) syntax here, while Python uses (?P<name>...) — a detail worth checking when moving a pattern between languages.
How to use
- Enter a pattern: type your expression in the "Regular expression" box. The surrounding slashes and flags are just a hint — you do not need to type the slashes yourself.
- Choose flags: tick g (global), i (ignore case), m (multiline), s (dot matches newline), u (Unicode) or y (sticky) as needed.
- Insert a snippet: if you are unsure about the syntax, click a button under "Common patterns" to insert rules for emails, phone numbers, dates and more, then adjust them.
- Inspect matches: type or paste text into "Test text". All matches are highlighted on the right, and each match’s position and capture groups are listed below.
- Replace text: switch to "Replace" mode, fill in the replacement under "Replace with" (supports $1, $<name> and more) to get the full replaced text, ready to copy.
- Build in small pieces: start with the simplest pattern that matches one case, confirm it, then add groups and quantifiers one at a time instead of writing the whole expression at once.
- Test hostile input: try empty strings, very long repetitions, Unicode text and unexpected line breaks. A pattern that works on the happy path can still hang on malformed data.
FAQ
Are my pattern and text uploaded?
No. Matching and replacing use the regex engine built into your browser and run entirely on your device, with no network requests. Everything disappears as soon as you refresh or close the page.
Why does my regex say it is invalid?
Common causes include unpaired parentheses or brackets, incorrect escaping and conflicting flags. The tool shows the specific error message so you can fix it accordingly.
What does the g flag do?
g means global matching: it finds every matching occurrence instead of just the first. Without it, replacements only affect the first match. To make inspection easier, the "Highlighted matches" panel always shows all matches.
How do I reference capture groups?
Wrap the part you want to capture in parentheses () to create a group. Reference it in the replacement with $1, $2 and so on, or define a named group with (?<name>...) and reference it with $<name>.
Can I use these regexes in JavaScript?
Yes. This tool follows JavaScript regex syntax, so once a pattern works here you can move the expression and its flags straight into your JS code.
Why does the dot not match line breaks?
By default . matches any character except line terminators such as \n. Enable the s (dotAll) flag, or use [\s\S], which matches absolutely any character including newlines.
What is the difference between greedy and lazy quantifiers?
A greedy quantifier such as .* consumes as much as possible and then backtracks; a lazy one such as .*? consumes as little as possible. For extracting the content between two quotes or tags, the lazy version is usually what you want, otherwise the match swallows everything up to the last occurrence.
How do I match Chinese characters?
With the u flag, \p{Script=Han} matches Han characters and is the most reliable option. The common range [\u4e00-\u9fa5] also works for basic CJK but misses extensions, rare characters and compatibility ideographs.
Why does my expression freeze the page?
Almost always catastrophic backtracking: nested quantifiers such as (a+)+ or (.*)* multiply the number of paths the engine tries. Simplify the pattern, make one quantifier bounded, anchor the match, or avoid overlapping alternatives.
Is lookbehind supported everywhere?
Lookahead is universal. Lookbehind (?<=...) was added to JavaScript more recently and is missing from older Safari versions and some other engines, so if the pattern must run in legacy browsers or on a server, test it there or rewrite it with a capture group.
How do I reference captured text in the replacement?
Use $1, $2 and so on for numbered groups, $<name> for named groups, $& for the entire match, and $$ for a literal dollar sign. Switch to Replace mode to see the result immediately.