JavaScript Regex Tester
Test JavaScript regular expressions online with live ECMAScript matching. Validate JS regex flags (g, i, m, s, u, y), preview capture groups, and debug patterns before deploying to frontend or Node.js code.
Why Use a JavaScript Regex Tester
JavaScript regex has ECMAScript-specific quirks: the u flag for Unicode property escapes, sticky matching with y, named capture groups using (?<name>), and the s (dotAll) flag added in ES2018. Testing patterns in a browser before embedding them in frontend validation, routing logic, or Node.js parsers saves debugging time. Verify that String.match(), String.matchAll(), and RegExp.exec() return exactly what you expect without running your full application.
- ECMAScript syntax: Validate
(?<name>)named groups and\k<name>backreferences - All JS flags: Test g, i, m, s, u, v, y behavior interactively
- Unicode support: Validate
\p{Letter}and\p{Script=Greek}with the u flag - Global vs sticky: Understand how
gandyflags change match behavior - Real-time iteration: No Node.js or browser console required
Choose the Right Variant
- This page: ECMAScript patterns with JS-specific flags and named group syntax
- Python Regex Tester: Python re module with
(?P<name>)syntax - Java Regex Tester: java.util.regex patterns for Java and Android
- Email Regex Tester: Validate email address patterns specifically
Step-by-Step Tutorial
- Enter your JS regex pattern โ e.g.
(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2}) - Set flags as needed:
gfor all matches,ifor case-insensitive,mfor multiline - Paste test strings โ e.g.
2024-03-15,Birthday: 1990-07-22 - See matches highlighted in real-time with named group values displayed
- Test edge cases: empty strings, special characters, very long inputs
- Copy the validated pattern as a JS regex literal:
/(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/g
Real-World Use Case
A frontend developer builds a form that accepts version strings like v1.2.3, 2.0.0-beta.1, and 1.0.0-rc.2. They need to validate semver format and extract major, minor, and patch components. In the JS regex tester they build /^v?(?<major>\d+)\.(?<minor>\d+)\.(?<patch>\d+)(?:-(?<prerelease>[\w.]+))?$/ and test it against 25 version strings. The tester shows named group extraction instantly โ they discover the pattern rejects 1.0.0+build.1 (build metadata). They add (?:\+[\w.]+)? to the pattern, retest, and all 25 cases pass. They copy the regex into their Zod schema validator.
JavaScript Regex Syntax Reference
- Named groups:
(?<name>pattern)โ access withmatch.groups.name - Named backreference:
\k<name>โ matches same text as the named group - Unicode property:
\p{Letter}withuflag โ matches any Unicode letter - dotAll flag (s): Makes
.match newlines โ added in ES2018 - Sticky flag (y): Matches only at
lastIndexโ useful for tokenizers - v flag (ES2024): Set notation and properties of strings โ
\p{RGI_Emoji}
Common JavaScript Regex Mistakes
- Forgetting the g flag: Without
g,String.match()returns only the first match - Infinite loop with g flag: Calling
exec()in a loop without advancinglastIndexcauses infinite loops - str.replace with function: The callback receives
(match, p1, p2, offset, string)โ easy to mis-order arguments - No u flag for Unicode: Without
u,\p{}property escapes throw a SyntaxError - JS named group syntax: Use
(?<name>)not(?P<name>)(Python/PCRE syntax)
Privacy and Data Handling
All regex matching runs locally in your browser using the JavaScript RegExp engine. Your patterns and test strings never leave your device. When testing against sensitive user data such as emails or phone numbers, use synthetic test cases rather than real customer data.
Frequently Asked Questions
Why does String.match() return different results with and without the g flag?
Without the g flag, String.match() returns an array containing the first match along with capture groups, index, and input โ similar to RegExp.exec(). With the g flag, it returns an array of all match strings but strips capture group details. To get all matches with capture groups, use String.matchAll() (ES2020) which returns an iterator of exec-style results for each match. This is the most common source of confusion when migrating regex from testing tools to production code.
What does the JavaScript u flag do for regex?
The u flag enables full Unicode mode. Without it, regex treats strings as a sequence of UTF-16 code units โ emoji and supplementary characters spanning two code units cause unexpected behavior. With u: Unicode property escapes \p{Letter} work, surrogate pairs are treated as single characters, and invalid escape sequences throw errors (catching typos like \e). Always use the u flag when working with international text, emoji, or any non-ASCII content.
How do JavaScript named capture groups work?
JavaScript uses (?<name>pattern) syntax (ES2018). Access captured values via match.groups.name or destructuring: const { year, month } = str.match(pattern).groups. Named groups also work in String.replace(): str.replace(/(?<y>\d{4})-(?<m>\d{2})/, '$<m>/$<y>'). Unlike Python, JS uses \k<name> for backreferences (not (?P=name)). Named groups make regex significantly more readable and maintainable than positional group indices.
What is the difference between the g and y flags?
Both flags enable multiple-match mode, but differ in anchoring. The g (global) flag searches anywhere in the string from lastIndex forward. The y (sticky) flag requires a match exactly at lastIndex โ it fails if the match doesn't start precisely there. The sticky flag is useful for hand-written parsers and tokenizers where you process a string left-to-right and need to guarantee no gaps between tokens. With g, skipped characters between matches are silently ignored.