Regular expressions in practice

A regular expression describes a set of strings: \d{4}-\d{2}-\d{2} matches dates written as 2025-03-14, [A-Z]{2}\d{2} the start of a UK postcode-like code. Parentheses create capture groups, which the replacement can reuse as $1, $2 or by name when the group has one.

Flags change the behaviour: g finds every match instead of the first, i ignores case, m makes ^ and $ work on each line, s lets the dot match line breaks, u enables Unicode classes such as \p{L}. Without g, replacement changes only the first occurrence.

Some patterns, such as (a+)+$ or (\w+\s?)*$, try an exponential number of combinations on a text that almost matches and can freeze a browser or a server: that is ReDoS. Here the test runs in a separate process and is stopped after one second, so the page stays usable and tells you about the problem.

Common mistakes

  • Forgetting to double the backslashes when the regex goes inside a string in code: here you write \d, in a JavaScript string "\\d".
  • Using .* where .*? is needed: the greedy quantifier takes everything up to the last possible occurrence.
  • Nesting quantifiers as in (a+)+ or (a|a)*: the most common cause of catastrophic backtracking.

Frequently asked questions

Which regex engine does it use?

Your browser's JavaScript (ECMAScript) engine. It is very close to PCRE, used by PHP, but some features, such as atomic groups and possessive quantifiers, are missing.

How do I use a group in the replacement?

With $1, $2… for numbered groups, $ followed by the name in angle brackets for named ones and $& for the whole match. A literal dollar sign is written $$.

Is the text I type sent anywhere?

No: everything happens in the browser. Pattern and text only go into the page address, so it can be shared, and only when they are short.

How this calculation works

The pattern is compiled with new RegExp(pattern, flags) and applied with String.prototype.matchAll, always with the g flag added to walk the text; without g in the request only the first match is kept. Replacement uses String.prototype.replace with the chosen flags. The run happens in a Web Worker, terminated if it does not answer within 1 second; at most 1000 matches are listed.