DevRivet

JavaScript Regex Tester

Test JavaScript regular expressions online: see every match, its index and capture groups, highlighted in your text.

Your data stays in your browser. Nothing is uploaded to a server.

Flags

What is a regular expression?

A regular expression (regex) is a pattern that describes a set of strings. Developers use them to validate input, search logs, and extract pieces of text — for example, every number in a line or every email-like token in a file. This tester uses your browser's built-in JavaScript RegExp engine, so results match what you'll get in JavaScript and Node.js. Other engines (PCRE, Python, Java) differ in some details.

How to use the Regex Tester

Type the pattern without the surrounding slashes, pick the flags you need, paste your test string, and press Test (or Enter in the pattern field). You'll see whether it matched, how many matches there are, each match's text and index, and the value of every capture group. Up to 1,000 matches are shown.

JavaScript regex flags

In JavaScript, flags go after the closing slash of a regex literal (/\d+/gi) or in the second argument of the constructor (new RegExp("\\d+", "gi")). Read them back from regex.flags.

  • g — global: find all matches instead of stopping at the first. Required by String.prototype.matchAll and replaceAll with a regex.
  • i — ignore case: a also matches A.
  • m — multiline: ^ and $ match at the start and end of each line. For example, ^\w+ on two lines one / two finds only one without m, and both words with it.
  • s — dotAll: . also matches newline characters.
  • u — unicode: treats emoji and other astral characters as one character and enables \p{...} property escapes.
  • y — sticky: a match must start exactly at regex.lastIndex instead of searching ahead. With lastIndex = 3, /foo/y matches barfoo, but from index 0 it doesn't. It is mainly used for writing tokenizers that consume input step by step, so this tester doesn't offer it — test the pattern without y, then add it in your code.

Common regex patterns

Paste any of these into the tester (without slashes) with the g flag. They are practical starting points, not full validators.

  • Number (optional sign and decimals): -?\d+(?:\.\d+)? — in Temp -12.5 C, max 30 matches -12.5 and 30.
  • Email-like token: [^\s@]+@[^\s@]+\.[^\s@]+ — finds [email protected]. Real email validation is better left to sending a confirmation message.
  • ISO date (YYYY-MM-DD): \d{4}-\d{2}-\d{2} — matches 2025-03-14. It checks the shape only, so 2025-13-99 also matches.
  • Hex color: #(?:[0-9a-fA-F]{3}){1,2}\b — matches #fff and #1a2b3c.
  • IPv4 address (shape only): \b(?:\d{1,3}\.){3}\d{1,3}\b — matches 192.168.0.1; it doesn't check that each part is 255 or less.
  • Repeated word: \b(\w+)\s+\1\b with gi — in This is is a test Test finds is is and test Test.
  • URL slug: ^[a-z0-9]+(?:-[a-z0-9]+)*$ — matches my-post-2025 but not My Post.
  • Leading or trailing whitespace: ^\s+|\s+$ — the pattern behind a manual trim, e.g. str.replace(/^\s+|\s+$/g, "").

Example

The pattern \d+ with the g flag on the text Order 123, Order 456 finds 2 matches: 123 at index 6 and 456 at index 17. Wrapping part of a pattern in parentheses, like Order (\d+), captures just the number as group 1.

Named capture groups label each group. The pattern (?<year>\d{4})-(?<month>\d{2}) with the g flag on Released 2024-01, patched 2025-12 finds 2 matches: 2024-01 at index 9 (year 2024, month 01) and 2025-12 at index 26 (year 2025, month 12). The results list each group by number and name, e.g. Group 1 <year>.

FAQ

Is my text sent anywhere?

No. Patterns and test strings are evaluated entirely in your browser.

Why does my pattern show "Invalid regular expression"?

The pattern has a syntax error, such as an unclosed [ or (, or a quantifier with nothing to repeat. The message from the JavaScript engine points to the problem.

Why did my regex time out?

Some patterns take exponentially long on certain inputs — known as catastrophic backtracking. For example, (a+)+$ against a long run of a characters followed by ! can run for minutes. This tester evaluates every pattern in a Web Worker, separate from the page, and stops it after about 1.5 seconds with the message "Regular expression timed out. The pattern may cause excessive backtracking." Your tab stays responsive, and you can edit the pattern and test again. Nested quantifiers like (a+)+ are the usual cause.

Does it work in every browser?

It needs Web Worker support, which all current major browsers have. If Web Workers are unavailable, the tester shows an error instead of running the pattern, rather than risk freezing the page.