What a regular expression is
A regular expression, or regex, is a compact pattern that describes a set of strings. Programmers use them to search text, validate input such as email addresses or postcodes, and extract or replace parts of a string. Because the syntax is dense, it pays to test a pattern against sample text before putting it in code. This tester uses the JavaScript regular expression engine built into your browser, so it behaves exactly as the pattern would in a web page or in Node.js.
Reading the results
Every match is highlighted in the preview, and the list underneath gives the match number, the character index where it starts, the matched text and the values of any capture groups. Groups are the parts of the pattern wrapped in parentheses. In the default example, the pattern (\w+)@(\w+)\.com finds two addresses, and the groups give the user name and the domain name of each. If the pattern has a syntax error, such as an unclosed bracket, the browser's error message is displayed instead.
The flags
- g (global): find all matches rather than stopping at the first. Without it only the first match is shown.
- i (ignore case): treat upper and lower case letters alike.
- m (multiline): make ^ and $ match at the start and end of each line, not just the whole string.
- s (dotAll): let the dot match line breaks as well.
- u (unicode): treat the pattern and text as full Unicode, needed for emoji and for property escapes like \p{L}.
Common building blocks
\d matches a digit, \w a letter, digit or underscore, \s a space character, and a dot almost anything. Quantifiers control repetition: * means zero or more, + one or more, ? zero or one, and {2,4} between two and four. Adding ? after a quantifier makes it lazy so it matches as little as possible. Anchors ^ and $ pin the match to the start or end. Parentheses create groups, and a pipe means "or".
Cautions
Some patterns with nested repetition, such as (a+)+$, can take exponential time on certain input and freeze the page. If your browser tab stops responding, simplify the pattern. The tester caps the list at 1,000 matches. Regex flavours differ between languages, so a pattern that works in JavaScript may need changes in Python, PHP or Java. Everything runs locally and your text is not sent anywhere.