Regex Tester for JavaScript Regular Expressions
Enter a pattern, tick the flags and paste your test text. The matches are highlighted and listed with their positions and capture groups. It runs in your browser and nothing is sent.
Last reviewed
2 matches
Dates: 2024-03-15 and 2025-12-01.
| # | Match | Position | Groups |
|---|---|---|---|
| 1 | 2024-03-15 | 7–17 | 1: 20242: 033: 15 |
| 2 | 2025-12-01 | 22–32 | 1: 20252: 123: 01 |
- Matches
- 2
- Flags
- g
What a regular expression is
A regular expression (regex) is a pattern that describes a set of strings. You use it to find, check or pull apart text: does this look like an email address, which parts of the log are dates, where are the digits. This tester uses the JavaScript flavour, running in your browser’s own engine.
Flags
Flags change how the pattern is applied. MDN lists them as properties of a RegExp:
- g (global): find all matches, not just the first.
- i (ignore case): ignore upper and lower case.
- m (multiline):
^and$also match at each line break. - s (dot all): the dot matches line terminators too.
- u (unicode): enable Unicode features.
- y (sticky): match only at the position where the last match ended.
Syntax cheat sheet
Taken from the MDN syntax cheat sheet:
| Syntax | Meaning |
|---|---|
\d |
a digit, the same as [0-9] |
\w |
a letter, digit or underscore, the same as [A-Za-z0-9_] |
\s |
a white space character: space, tab, line feed and others |
. |
any one character except a line terminator |
[abc] and [^abc] |
any one of a, b, c; or any character except those |
^ and $ |
the start and the end of the input (or of a line with the m flag) |
\b |
a word boundary |
x* and x+ and x? |
x zero or more times; one or more times; zero or one time |
x{n} and x{n,m} |
x exactly n times; between n and m times |
x*? |
a lazy quantifier, which matches as little as possible |
(x) and (?:x) |
a capturing group; a group that does not remember the match |
(?<name>x) |
a named capturing group |
x(?=y) and x(?!y) |
x only if followed by y; only if not followed by y |
(?<=y)x and (?<!y)x |
x only if preceded by y; only if not preceded by y |
x or y written as x|y |
either x or y |
Worked examples
Dates. The pattern (\d{4})-(\d{2})-(\d{2}) on the text “Dates: 2024-03-15 and 2025-12-01.” finds 2 matches. The first is 2024-03-15 at positions 7 to 17, with the groups 2024, 03 and 15. The second is 2025-12-01 at positions 22 to 32.
Greedy and lazy. On the text <foo> <bar> new </bar> </foo> the pattern <.*> is greedy and matches the whole of it, from the first < to the last >. The lazy pattern <.*?> matches just <foo>. MDN uses this very example.
Named groups. The pattern (?<first>\w+) (?<last>\w+) on “Maria Cruz” gives the groups first = Maria and last = Cruz.
Anchors. With the m flag, ^\d+$ matches every line that is only digits. Without it, ^ and $ match only at the start and end of the whole text.
Tips
- Start simple. Build a pattern step by step and watch the highlights.
- Escape special characters. To match a literal dot, write
\.. - Prefer specific classes to the dot.
[^"]*is safer than.*. - Beware of backtracking. Nested quantifiers such as
(a+)+can take a very long time on a text that almost matches. - The pattern is JavaScript. Check other languages’ documentation for differences.
Common mistakes
- Forgetting the g flag and wondering why only one match shows.
- Using
*when you mean+.x*also matches nothing at all. - Not escaping the dot, parentheses or brackets.
- Expecting the dot to match line breaks. Use the s flag.
- Testing on a different text from the one you will use.
Related tools: the JSON formatter, the URL encoder and decoder, and the cron expression generator, which also uses a pattern language.
More worked examples
Times of day. The pattern \b([01]?\d|2[0-3]):[0-5]\d\b on “Meet at 9:30, then 18:45 and 25:10.” finds 2 matches: 9:30 at positions 8 to 12 and 18:45 at positions 19 to 24. The 25:10 is rejected, because 25 is not an hour.
Optional letters. With the i flag, colou?r finds Color, colour and COLOR in “Color, colour, COLOR, colr”, and misses the misspelt colr.
Word boundaries. On “cat concatenate”, cat finds 2 matches, because it is also inside concatenate, and \bcat\b finds only 1.
Greedy digits. On “a1b22c333”, \d+ finds 1, 22 and 333. The lazy \d+? finds six matches, one for each digit, because a lazy quantifier stops as soon as it can.
How it is calculated
Nothing is calculated by this page: the pattern is handed to the browser’s RegExp engine and the matches, with the d flag’s indices, are listed as the engine returns them. The match list shows up to 500 matches.
Frequently asked questions
Which regex flavour does this use?
JavaScript's. The pattern runs in your browser's own RegExp engine, so the matches are exactly what JavaScript code would find. Other languages, such as Python, PCRE and Java, differ in details like lookbehind and named group syntax.
What do the flags mean?
g finds all matches, i ignores case, m makes ^ and $ match at each line, s lets the dot match line breaks, u treats the text as Unicode, and y matches only where the last match ended.
What is the difference between greedy and lazy matching?
Quantifiers such as * and + are greedy by default and match as much as they can. Adding ? makes them lazy, so they stop as early as possible. In <foo> <bar>, the pattern <.*> matches everything but <.*?> matches just <foo>.
How do I get a capture group?
Put brackets around part of the pattern. (\d+) remembers the digits it matched. Name a group with (?<year>\d{4}) and it appears under its name. The match table lists every group for every match.
Why does my pattern match nothing?
Check the flags and the anchors. Without i, case matters. Without m, ^ and $ only match at the very start and end of the text. Special characters such as . ( ) [ ] + * ? need a backslash if you mean them literally.
Is my text sent anywhere?
No. The pattern and the text stay in your browser, are not saved and are not added to the page address. A pattern that backtracks a lot can make the page slow, and refreshing the page stops it.
Related calculators
- JSON Formatter and ValidatorPaste JSON to check it against RFC 8259 and format it with 2 or 4 spaces or a tab, or minify it.
- URL Encoder and DecoderPaste text to percent-encode it for a URL, or paste an encoded string to read it.
- Base64 Encoder and DecoderType or paste text to encode it as base64, or paste base64 to decode it.
- Cron Expression GeneratorFill in the minute, hour, day of month, month and day of week, or start from a common schedule.
- Hash GeneratorType or paste text and see its MD5, SHA-1, SHA-256, SHA-384 and SHA-512 hashes at once, as hex or base64.
- Epoch and Unix Timestamp ConverterEnter a Unix timestamp to see the date, or enter a date and time to get the timestamp.