A regular expression tester shows how a pattern behaves against sample text before you place it in an application. This tool uses the browser’s JavaScript regex engine, highlights matches live, and exposes indexes and capture groups. Results therefore follow ECMAScript syntax rather than the rules of PCRE, Python, Java, or another regex flavor.
How to Use the Regex Tester
Test an ECMAScript pattern and inspect every reported match.
- Enter the expression in the Pattern input.
- Enable the
g,i,m,s,u, oryflag checkboxes as needed. - Enter sample content in the Test string textarea.
- Review the matches highlighted live in the test string.
- Inspect each match, its index, and any numbered or named capture groups in the match list.
JavaScript Regular Expression Syntax Cheat Sheet
A JavaScript regular expression describes text to match. Literal characters generally match themselves, while metacharacters define positions, choices, repetitions, or character sets. Escape a metacharacter with a backslash when it should be treated literally. In this tester, enter the pattern itself rather than surrounding it with slash delimiters.
| Syntax | Meaning | Example |
|---|---|---|
. |
Any character except a line terminator, unless s is set |
a.c |
^ and $ |
Start and end assertions | ^done$ |
[abc] |
One listed character | [aeiou] |
[^abc] |
One character not listed | [^0-9] |
\d, \w, \s |
Digit, word, or whitespace class | \d+ |
*, +, ? |
Zero or more, one or more, zero or one | colou?r |
{n,m} |
Repetition range | \d{2,4} |
a|b |
Alternation | cat|dog |
Prefer a format-specific parser when input has nested grammar or complex escaping rules.
JavaScript Regex Flags and Match Behavior
Flags change how the ECMAScript engine interprets a pattern. The g flag searches for all matches and advances through the string. Without it, JavaScript normally returns only the first match. The i flag enables case-insensitive comparison, while m makes ^ and $ apply at line boundaries as well as string boundaries.
The s flag, called dotAll, lets . match line terminators. The u flag enables Unicode-aware parsing and changes how some code points, escapes, and quantifiers behave. The y flag performs a sticky match starting exactly at the engine’s current index; it is useful in tokenizers but can surprise users expecting a general search.
Case folding is not identical to locale-aware comparison, and \w does not represent every Unicode letter. Test representative multilingual and multiline samples. In code, remember that stateful g or y use can affect lastIndex between calls.
Capture Groups, Named Groups, and Lookarounds
Parentheses create a numbered capture group, allowing code to retrieve a specific part of the match. For example, (\d{4})-(\d{2})-(\d{2}) captures year, month, and day separately. A named group such as (?<year>\d{4}) makes the result easier to understand when a pattern contains several meaningful fields. Use (?:...) for grouping without storing a capture.
Lookarounds assert context without consuming it. A positive lookahead, (?=...), requires following text; a negative lookahead, (?!...), rejects it. JavaScript also supports positive and negative lookbehind in modern engines, written (?<=...) and (?<!...). Browser support can matter when code targets older environments.
Repeated groups usually retain the final captured value rather than every repetition. Optional groups may be unmatched, and alternation changes which groups participate. For structured formats such as JSON, use the JSON formatter and a real parser rather than recognizing arbitrary nesting with one expression.
Avoiding Catastrophic Backtracking and ReDoS
Some backtracking regex engines can explore a rapidly growing number of paths when a near-match fails. Patterns with nested ambiguous quantifiers, overlapping alternatives, or repeated groups are common causes. For example, a shape like (a+)+$ can become expensive on a long run of a characters followed by a different character. If attacker-controlled input reaches such a pattern, the delay can become a regular expression denial-of-service risk.
Keep repetitions unambiguous. Replace broad constructs such as .* with a constrained character class when the delimiter is known, avoid nesting quantifiers, and make alternatives distinguishable near their beginning. Limit input length and execution scope where possible. Test long non-matching strings, not only short successful examples, because failure often triggers the worst behavior.
JavaScript does not provide a portable per-match timeout through the regex API. For untrusted patterns, consider an engine with stronger execution guarantees or isolate work from the main interface. This tester runs each match in a Web Worker and stops it after one second, so a runaway pattern cannot freeze the page. A tester can expose slow cases but cannot prove safe worst-case performance.
Frequently Asked Questions
Why does my regex work in Python or PCRE but not in JavaScript?
Regular expression flavors differ in supported syntax, character classes, group features, flags, and escaping. This tester uses the browser’s ECMAScript engine, so constructs specific to Python, PCRE, Java, or .NET may fail or behave differently. Check JavaScript documentation and remember that a pattern copied from source code may contain an extra string-literal escaping layer.
What is the difference between the g and m flags?
The g flag finds successive matches throughout the input. The m flag changes only the behavior of ^ and $, allowing them to assert positions around line terminators as well as the beginning and end of the full string. Multiline mode does not automatically return multiple matches; use g when you need a match list.
How do I match a literal period, bracket, or backslash?
Escape a regex metacharacter with a backslash, such as \. for a period or \[ for an opening bracket. A literal backslash is represented as \\ in the pattern. When writing the same expression inside a JavaScript string, the string syntax may require another escaping layer; regex literals and pattern input fields do not have identical notation.
Why are my capture groups empty or undefined?
A capture can be absent when its group is optional, belongs to an alternation branch that did not match, or occurs inside a repetition whose final iteration did not capture text. Review the full match and group boundaries. Named groups improve readability, but they follow the same participation rules as numbered groups and do not force content to exist.
References: MDN: Regular expressions · ECMAScript specification: RegExp objects · OWASP: Regular expression Denial of Service.