What is a regular expression?
A regular expression (regex) is a pattern that describes text. \d{4}-\d{2}-\d{2} describes a date such as 2026-10-20; ^\s+|\s+$ describes whitespace at the start or end of a string. Regexes are built into almost every programming language and editor, and are used to validate input, search logs, extract data and find-and-replace across files.
How to test a regex
- Type your pattern in Pattern, without the surrounding slashes, or choose one from Common patterns.
- Tick the flags you need. Global finds every match instead of just the first.
- Paste sample text into Test string. Matches are highlighted as you type, alternating between two shades so that adjacent matches are easy to tell apart.
- Open Matches to see each match’s position and capture groups, Replace to try a replacement, or Code for JavaScript you can paste into your project.
Regex syntax cheat sheet
Characters and classes
.any character except a line break (any character at all with thesflag)\da digit,\wa word character (letter, digit or underscore),\swhitespace;\D,\W,\Sare their opposites[abc]one of a, b or c;[^abc]anything else;[a-z]a range\ba word boundary,\.a literal dot
Anchors
^start of the string (or line, withm),$end of the string (or line)
Quantifiers
*zero or more,+one or more,?zero or one{3}exactly three,{2,5}two to five,{2,}two or more- Add
?to make them lazy, matching as little as possible:.*?
Groups and alternation
(…)a capturing group, available as$1in replacements(?<name>…)a named group, available as$<name>(?:…)a group that does not capturea|beither a or b
Lookaround (match a position without consuming text)
(?=…)followed by,(?!…)not followed by(?<=…)preceded by,(?<!…)not preceded by
Catastrophic backtracking
When a match fails, the regex engine backtracks and tries other ways to divide the text between the parts of the pattern. With nested quantifiers such as (a+)+$, the number of ways grows exponentially. Against 30 a characters followed by !, that is over a billion attempts, enough to freeze a browser tab or take down a server. This is the basis of ReDoS (regular expression denial of service) attacks.
This tester runs every pattern in a background worker and stops it after one second, so you can find these patterns safely. To fix one, remove the nesting (a+$ instead of (a+)+$) or make each repeated part unable to match the same text as its neighbour.
Using regular expressions in JavaScript
const date = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/;
date.test('Due 2026-10-20'); // true
'Due 2026-10-20'.match(date).groups.year; // '2026'
'Due 2026-10-20'.replace(date, '$<day>/$<month>/$<year>'); // 'Due 20/10/2026'
for (const m of 'a1 b22'.matchAll(/\d+/g)) console.log(m[0], m.index);
matchAll and replaceAll need the g flag. When building a pattern from user input with new RegExp(…), escape special characters first, or a stray ( will throw an error.
How JavaScript regex differs from other languages
- Python writes named groups as
(?P<name>…)and refers to them as(?P=name); JavaScript uses(?<name>…)and\k<name>. - PCRE (PHP, many command-line tools) has possessive quantifiers such as
a++and atomic groups; JavaScript does not. - grep and sed use POSIX syntax by default, where
+and?may need a backslash or the-Eoption.