JavaScript Regex Tester

Test regular expressions written for JavaScript (ECMAScript RegExp) — with warnings for anything that works differently there, and JavaScript code to copy.

//g

JavaScript regex syntax at a glance

Literal
/pattern/flags or new RegExp("pattern", "flags")
Named group
(?<name>…), back-reference \k<name>
Replacement
$1, $<name>, $& (whole match)
Flags
g, i, m, s (dotAll), u / v (Unicode), y (sticky), d (indices)
Find all
"text".matchAll(/…/g)

What's different in JavaScript

  • →Lookbehind (?<=…) works with any length since ES2018.
  • →There are no atomic groups, possessive quantifiers or inline (?i) flags — put flags after the closing slash.
  • →\d and \w are ASCII-only, even with the u flag; use \p{Nd} or \p{L} with u for Unicode.
  • →^ and $ match only at the very start and end unless you add the m flag — $ does not allow a trailing newline.

Frequently asked

Why does my regex only find the first match?

Without the g flag, match() and replace() stop after the first match. Add g, or use matchAll() with a g regex to loop over every match with its groups.

How do I build a JavaScript regex from a variable?

Use new RegExp(text, flags), and escape special characters in the text first — newer engines have RegExp.escape(), otherwise text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&").

ToolNinja