Appearance

Regex Tester

/regex-tester/

Flags

Related tools

Comments

0/500

About this tool

Why use a regex tester

A regular expression is a small language for finding the parts of a string that follow a rule. It shows up throughout development work, from validating email addresses and phone numbers in sign-up forms to pulling IP addresses or error codes out of logs and bulk replacing in an editor.

Read moreShow less

This regex tester highlights matches as soon as you enter a pattern and test string, and lists the capture group values of each match in order. The default example (\d{3})-(\d{4})-(\d{4}), which matches Korean mobile numbers, run against two lines, '홍길동 010-1234-5678' and '김철수 010-9876-5432', gives 2 matches, and groups 1, 2 and 3 of the first match are 010, 1234 and 5678. Copy copies only the matched strings, one per line.

Patterns run on the RegExp engine built into the browser you are using. It is not an emulated engine, so results here match how your frontend code will behave.

Regex flags and how the tester runs your pattern

Five flags can be toggled: g, i, m, s and u. g searches globally; without it, matching stops at the first match. i ignores case, and m makes ^ and $ match at the start and end of each line rather than of the whole string. With ^\w+ on the two lines 'abc' and 'def', g alone finds 1 match and g with m finds 2. s (dotAll) lets . match line breaks, so a.b does not match 'a, newline, b' without s but does with it.

The u flag treats the string as Unicode code points instead of UTF-16 code units. The emoji 😀 is stored as two code units in a JavaScript string, so a global search for . without u splits it into 2 matches, while with u it is 1. ES2018 syntax such as named groups (?<year>\d{4}) and lookbehind (?<=…) works in current browsers, and named groups are listed together with numbered ones.

Matching runs in a Web Worker, separate from the page. Once you stop typing for 0.15 seconds a worker is started, and if it does not finish within 1 second it is terminated and a timeout is reported. Patterns that can match an empty string, such as a*, advance one position at a time so they cannot loop forever, and up to 1,000 matches are computed, with the first 50 shown in the group list.

Refining a regex for different situations

For validation patterns, anchoring the start and end with ^ and $ is an important habit. \d{3}-\d{4}-\d{4} on its own also matches part of a string like 'abc010-1234-5678xyz', letting it pass the check.

For log analysis or multi-line replacement, first confirm how the m flag changes the meaning of ^ and $. For email, a regex that faithfully implements RFC 5322 is extremely long and complex, so in practice many teams filter the format with something like [^\s@]+@[^\s@]+\.[^\s@]+ and leave real verification to a confirmation email.

Python, Java and PHP (PCRE) users should watch for syntax differences. Python writes named groups as (?P<name>…), and the possessive quantifiers (a++) and atomic groups ((?>…)) supported by Java and PCRE are not part of standard JavaScript RegExp. The core syntax is mostly shared, so sketch the pattern here, then test it once more in the target language.

Catastrophic backtracking and other regex pitfalls

Most regex engines, JavaScript's included, use backtracking: at each branch they try one path first and back up to try another on failure. With a repetition inside a repetition, as in (a+)+$, the number of ways to split the same string grows exponentially with its length. A string of many a characters followed by ! never matches, but the engine tries nearly every possible split before concluding that it fails.

Running this site's matching function in Node.js took about 0.8 seconds with 26 a characters and about 3.3 seconds with 28 (on the test PC), roughly doubling with each extra character. This is called catastrophic backtracking, and deliberately sending such input to stall a server is a ReDoS attack. Cloudflare's major outage in July 2019 was caused by excessive backtracking in a regex in a WAF rule.

The 1-second limit only protects this page; server code has no such safeguard by default. After a timeout, reduce (a+)+ to a+, remove nesting like (.*)*, and narrow character ranges so repeated parts do not compete for the same characters. The y, d and v flags are not available. Patterns and text run only in this browser and are not sent to a server; the pattern, flags and text are kept in this browser for next time, Reset restores the default example, and the last 5 patterns that produced a result can be reloaded from Recent.

FAQ

Which regex flavor does this tester use?

JavaScript (ECMAScript) regular expressions, run by your browser's RegExp engine. ES2018 syntax such as named groups, lookbehind and the s flag works in current browsers. Very old browsers may report some syntax as an error.

Why does my regex time out after 1 second?

Backtracking made the amount of work explode on this input. Collapsing nested repetition such as (a+)+ or (.*)* into one, and narrowing .* to a range like [^,]*, solves most cases. Using the same pattern on a server carries a ReDoS risk, so it should be fixed.

Why do I get only one match without the g flag?

Without g, JavaScript RegExp stops at the first match, and this tool shows exactly that behavior. Turn on g to see every match and its capture groups. For validation, where you only need to know whether it matches, leaving g off makes the result easier to read.

What regex matches a Korean mobile phone number?

To accept only the hyphenated format, use ^01[016789]-\d{3,4}-\d{4}$. To also accept input without hyphens, make them optional with ^01[016789]-?\d{3,4}-?\d{4}$. When checking several lines at once, turn on the m flag so ^ and $ apply to each line.

Can I test Python or Java regex here?

The core syntax (character classes, quantifiers, groups, lookahead) is mostly the same, so it works for checking the skeleton. Syntax that JavaScript lacks, such as Python's (?P<name>…) or Java's possessive quantifiers (a++), causes an error. Check the final pattern in the language you will actually use.

Is my pattern or text saved?

Nothing is sent to a server; patterns and text run only in a Web Worker inside this browser. The pattern, flags, text and the last 5 entries are kept in this browser's localStorage for next time, and Reset and Clear remove them.