Regex Tester & Debugger

Share:
Developer Tools

Regex Tester & Debugger

Test and debug regular expressions in real time — with match highlighting, groups and a built-in reference.

By Checked against sources by Last reviewed DEBUG DEVELOPER PATTERN REGEX TEST
Your browser is too old for this tool’s interactive features. Please update to Safari 13.1, Chrome or Edge 80, Firefox 74 or newer.

Regex Tester Tool

Flags:

Your test string appears here with matches highlighted…
Advertisement
After results · AD-W1 Responsive · Post-tool — peak engagement

How to Use the Regex Tester

Enter your regex pattern

Type your pattern between the two slashes, without the slashes themselves. This tester uses your browser's own JavaScript (ECMAScript) regex engine, so a pattern written for PCRE, Python or .NET may behave differently here. Results update as you type.

Paste your test string

Paste the text to test into the Test String box. Matches are highlighted in five alternating colours, and an empty match (a pattern such as \b or x*) shows as a thin red bar. Matching runs in a background worker: if it takes longer than 1.5 seconds it is stopped, and the page says why, instead of freezing.

Toggle flags as needed

The buttons are the eight flags ECMAScript defines: g all matches, i ignore case, m ^ and $ at line breaks, s dot matches line breaks, u and v Unicode modes (only one at a time), y sticky, and d indices. A flag your browser does not support is greyed out.

Use the Quick Patterns library

The Common Patterns panel inserts a starting point: email, URL, IPv4, hex colour, Singapore phone, postal code and NRIC/FIN shapes, and a deliberately catastrophic pattern to show the time limit at work. They check shape only. The NRIC pattern does not verify the check letter, and the date pattern accepts 31/02/2026.

Advertisement
After how-to · AD-W2 Responsive

Regular Expressions — The Universal Language of Pattern Matching

Regular Expressions: From Zero to Useful in 10 Minutes

A regular expression (regex) is a sequence of characters that defines a search pattern. At its most basic, a regex is just a literal string: the pattern hello matches the exact text "hello". From there, the power multiplies rapidly. The dot metacharacter . matches any single character except a newline. Quantifiers control repetition: * means zero or more, + means one or more, and ? means zero or one. Character classes such as [a-z] or [0-9] match any character within the specified set, while anchors ^ and $ pin a match to the start or end of a line. Alternation with the pipe character | works like a logical OR, and parentheses () create capture groups that let you extract sub-portions of a match for reuse.

Every language has its own regex flavour, and the differences matter when you test a pattern in one place and run it in another. This page runs JavaScript's, defined by the ECMAScript specification (ECMA-262). Python has its own re module, PHP's preg_ functions use the PCRE library, Java has java.util.regex, and Go's regexp package accepts RE2 syntax and is guaranteed to run in time linear in the size of the input. PCRE supports features ECMAScript does not, such as possessive quantifiers and recursive patterns. ECMAScript itself keeps growing: ES2018 added named capture groups, the s flag and lookbehind, and ES2022 added the d flag for match indices.

Positions in the Match Details table are UTF-16 code-unit offsets, the same numbers JavaScript's match.index reports. An emoji or a rare CJK character outside the Basic Multilingual Plane counts as two. Turn on u or v and the dot, character classes and quantifiers treat such a character as one, and an empty match steps over it whole rather than landing between its two halves.

Capture Groups vs Non-Capturing Groups: When and Why They Matter

A capturing group (abc) does two things: it groups part of the pattern for quantifier purposes, and it creates a numbered reference — $1 in a replace string, or \1 as a backreference within the pattern itself. Backreferences are powerful for matching repeated patterns: the pattern (['"]).*?\1 matches a string delimited by either single or double quotes because \1 requires the closing delimiter to match whatever the opening one was.

When you only need the grouping behaviour without creating a reference, use a non-capturing group (?:abc). This is more efficient and avoids polluting your group index count. Named capture groups, added in ES2018, take readability further: (?<year>\d{4})-(?<month>\d{2}) lets you access matches as result.groups.year instead of result[1] — much clearer when the pattern has many groups. For context-dependent matching without consuming characters, use lookahead (?=abc) — which asserts that what follows matches the pattern — or lookbehind (?<=abc), which asserts what precedes. A practical example: the pattern \d+(?= dollars) matches a number only if it is immediately followed by the word " dollars", but the " dollars" part itself is not included in the match.

"A short regex can replace a page of string handling. A careless one can take a site down through catastrophic backtracking."

Validating input: anchor it, and do not parse HTML with it

A validation pattern needs anchors. Without ^ and $, \d{6} accepts abc123456xyz, because it finds six digits somewhere inside. OWASP's Input Validation Cheat Sheet advises defining what the application accepts and rejecting everything else, rather than trying to recognise every bad string. Two cautions follow. A regex can check the shape of an identifier but rarely its validity: a Singapore NRIC pattern cannot check the final letter, and a date pattern cannot know February has 28 or 29 days. And HTML nests, so a regular expression cannot parse it in general; use DOMParser in a browser instead.

Kleene in 1951, and the pattern that took Stack Overflow down for 34 minutes

01

The theory goes back to Stephen Kleene's RAND memorandum RM-704 (1951), Representation of Events in Nerve Nets and Finite Automata, on which events a finite automaton can respond to.

02

V8, the engine in Chrome and Node, matches with Irregexp, a backtracking engine. Its linear-time engine (V8 8.8, 2021) is experimental and off by default, which is why ^(a+)+$ can hang a tab and why this tester stops it.

03

With the g flag, test() and exec() start from lastIndex and move it on, so calling test() twice on the same string can return true and then false.

04

Go's regexp package accepts RE2 syntax and, in its documentation's words, is "guaranteed to run in time linear in the size of the input", so it cannot backtrack catastrophically.

05

RFC 5321 lets an email address have a quoted local part, so "user name"@example.com is valid. Most email regexes, including the simple one in this page's library, reject it.

06

Stack Overflow was offline for 34 minutes on 20 July 2016: a malformed post made a whitespace-trimming regex, ^[\s\u200c]+|[\s\u200c]+$, run on every home-page view, and the load balancer's health check took the servers out of rotation (Stack Exchange postmortem, archived copy).

Frequently Asked Questions

  • g finds every match instead of stopping at the first. i ignores case. m makes ^ and $ match at the start and end of each line. s (ES2018) lets the dot match line breaks. u treats the pattern and text as Unicode code points and enables \u{…} and \p{…}; v (ES2024) is a stricter successor that adds set operations inside [ ], and the two cannot be combined. y (sticky) only matches starting exactly at lastIndex. d (ES2022) records where each group matched; this tester shows group positions either way.
  • + is a quantifier meaning "one or more" — the preceding element must appear at least once. * means "zero or more" — the preceding element can be completely absent. So \d+ requires at least one digit, while \d* can match an empty string. For most validation use cases, + is the correct choice because you want at least one character to be present.
  • Escape the dot with a backslash: \. matches a literal period character. Without the backslash, . is a metacharacter that matches any character except newline. This is a very common mistake when writing patterns for file extensions (\.js$) or IP addresses (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}).
  • A capture group is a portion of your pattern wrapped in parentheses (). It serves two purposes: grouping for quantifiers, and capturing the matched text for later use. Each group is numbered from left to right starting at 1. In the Match Details table on this tester, groups are shown per match so you can see exactly what each one captured. In a replace string, reference them as $1, $2, etc. Named groups using (?<name>...) can be referenced as $<name>.
  • The pattern \b[STFGM]\d{7}[A-NP-RTUWXZ]\b checks the shape only: one of the five prefix letters, seven digits and a letter from the set used as check letters. It cannot tell whether the final letter is the right one for those digits, because that letter is computed from them, so a pattern accepts numbers that do not exist. Use the regex to find candidates in text, then check each one with code; the Singapore NRIC/FIN validator on this site does that check.
  • It happens when a backtracking engine, which JavaScript's is, can match the same text in a huge number of ways and has to try them all before it can fail. The classic case is ^(a+)+$ against a run of a's ending in b: every way of splitting the a's between the inner and outer + is tried, and the count doubles with each extra a. Try it from the Common Patterns panel; this tester stops it after 1.5 seconds. OWASP calls the attack that exploits this a ReDoS (Regular expression Denial of Service). The fix is to make the repetition unambiguous: ^a+$ matches the same strings at once.
  • Greedy quantifiers (*, +, {n,m}) match as many characters as possible. Lazy quantifiers (*?, +?, {n,m}?) match as few as possible. For example, with the input <b>bold</b> and <i>italic</i>, the greedy pattern <.*> matches everything from the first < to the last >. The lazy pattern <.*?> matches each individual tag. Lazy matching is much more common when parsing HTML-like structures.
  • Enable the m (multiline) flag to make ^ and $ match the start and end of each line. To make the dot match newlines (so a pattern can span multiple lines), also enable the s (dotAll) flag. Without s, you can work around the limitation with the character class [\s\S] which matches any whitespace or non-whitespace character — effectively matching everything including newlines. For example, [\s\S]*? is the multiline equivalent of a lazy .*?.
  • In the Replace tab, use $1, $2, etc. to insert the text captured by each group. For example, with pattern (\w+)\s(\w+) and replace string $2 $1, the words "first last" would become "last first". $& inserts the entire matched string, $` inserts the text before the match, and $' inserts the text after. For named groups defined as (?<name>...), use $<name> in the replace string.

Related News

You may be interested in these recent stories from our newsroom.

View all news →

Method & sources

How it computes

The browser's own RegExp (ECMAScript), flags d g i m s u v y as the engine supports them. Matches are listed as String.prototype.matchAll walks them (empty matches advance one code point under u/v, one UTF-16 unit otherwise), with group positions from d indices, in a Web Worker terminated after 1.5 s so catastrophic backtracking cannot freeze the page. Replace is String.prototype.replace.

What this tool implements

  • ECMA-262 RegExp semantics, run by the browser's engine rather than reimplemented; the eight flags in MDN's "dgimsuvy" order, each feature-detected; u and v refused together, as the engine does.
  • Time limit: each job runs in a dedicated Worker; if it has not answered in 1.5 s the page calls Worker.terminate() and says the pattern is backtracking catastrophically. If a Worker cannot start, the page says matching runs on the main thread and can hang.
  • Positions are UTF-16 code-unit offsets, as match.index reports; group positions use the d flag (ES2022) when the engine has it.
  • Corrected 2026-11-06: main-thread matching with no time limit; flags d u v y missing; surrogate-splitting empty-match advance; URL preset range %-?; hex preset {3,6}; (a+)+ example that does not backtrack; "PCRE is used by Python"; NRIC birth-year and modulo-11 claims with no source, cut.

Sources

Show 2 more

What can make this go out of date

  • None at runtime. The pattern and text go only to a Worker started from this site's own worker script; nothing is sent over the network.
  • Flag support is the visitor's browser's: a flag the engine rejects is disabled on the page, never emulated.
Advertisement
Pre-footer · AD-W3 728 × 90