Favorites

JavaScript Regex Tester

Write and test JavaScript regular expressions with instant feedback. Type a pattern, choose its flags and paste some text: every match is highlighted as you type, with its position and the value of each capture group, including named groups.

Switch to Replace or Split to preview replace() and split(), read a plain-English explanation of every part of the pattern, and copy ready-to-use JavaScript. It uses your browser’s own regex engine, so results match your code exactly, and a pattern that would freeze the page is stopped automatically.

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee

Regular expression

Mode

Test text

Matches

What the pattern means

JavaScript


		

Cheat sheet

Click an item to insert it into the pattern.
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee

About this tool

Using a regex in JavaScript

Write a regular expression between slashes, with any flags after the closing slash:

const re = /(?<year>\d{4})-(?<month>\d{2})/g;

for (const m of text.matchAll(re)) {
  console.log(m.groups.year, m.groups.month);
}

Use new RegExp('…', 'g') when the pattern is built from a string, and remember to double each backslash: '\\d+'.

The flags

  • g finds every match, i ignores case, m makes ^ and $ work on each line.
  • s lets . match line breaks, u turns on full Unicode support, v adds set operations to [ ].
  • y (sticky) only matches at lastIndex, and d records where each group starts and ends.

Catastrophic backtracking

Patterns with nested repeats, such as (a+)+$, can try an enormous number of combinations when a match fails, and freeze a page or a server. Here, matching runs in the background and stops after 1.5 seconds with an explanation. In your own code, avoid repeating a group that is itself repeated, and make each part as specific as you can.

Same engine as your code

The tester uses your browser’s built-in RegExp, so what you see here is exactly what your JavaScript will do in the same browser. That means it follows JavaScript’s rules, which differ in places from PHP, Python or other regex flavours.

Frequently Asked Questions

What is a regular expression?

A regular expression, or regex, is a pattern that describes text, such as “one or more digits” (\d+) or “an email address”. JavaScript uses them to search, check, extract and replace text with methods such as test(), match(), matchAll(), replace() and split().

How do I use this regex tester?

Type your pattern between the slashes, without the slashes themselves, and turn on the flags you need. Paste the text to search below it: matches are highlighted as you type and listed on the right with their capture groups. Click a match to select it in the text. You can also paste a whole regex such as /abc/gi into the empty pattern box.

Why does my regex only find the first match?

Without the g (global) flag, a regular expression stops at the first match, and replace() only replaces that one. Turn on g to find or replace every match, or use replaceAll(), which requires it.

What are capture groups and named groups?

Parentheses capture the part of the match inside them, so you can use it separately: in /(\d{4})-(\d{2})/ group 1 is the year and group 2 the month. Named groups, written (?<year>\d{4}), can be read as match.groups.year and used in a replacement as $<year>. Use (?:…) to group without capturing.

What does the m (multiline) flag do?

Normally ^ matches only at the very start of the text and $ only at the very end. With m, they also match at the start and end of every line, which is what you want when checking each line of a list separately.

How do I match a special character such as a dot or a bracket?

Put a backslash in front of it: \. matches a full stop, \( a bracket and \\ a backslash. The characters that need escaping are . * + ? ^ $ { } ( ) [ ] | \ /. Inside square brackets most of them lose their special meaning, so [.] also matches a dot.

What is the difference between greedy and lazy matching?

Repeats such as * and + are greedy: they match as much as they can. Adding ? makes them lazy, matching as little as possible. On <b>bold</b>, <.+> matches the whole thing, while <.+?> matches just <b>.

Why did the tester stop my pattern?

The pattern took more than 1.5 seconds on your text, which almost always means catastrophic backtracking: nested repeats like (a+)+ or (\w|\s)* that make the engine try a huge number of combinations. Running it in your own code could freeze the page, so it’s worth fixing by making the repeats more specific.

Will a regex that works here work in PHP or Python?

Often, but not always. This tester uses JavaScript’s regex engine, and other languages have different features and syntax, for example for named groups, lookbehind and Unicode. Test patterns for another language in a tester for that language.

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee