Settings

The pattern

Written the way you would write it in JavaScript, between the slashes. Flags go on the right.

The text to run it against

What it does

Waiting for a pattern.

What the pattern says, in words

Where the parser cannot read a construct it says so instead of guessing.

Patterns worth keeping

Each one says what it misses. Choosing one puts it in the pattern box above.

Cheat sheet

How it works

REGEX uses your browser's own regex engine, including flags, named groups and lookbehind, and answers the usual questions: why only the first match is found, why the dot skips new lines, and what greedy and lazy mean.

The guide: Why a regex works in regex101 but not in JavaScript

Questions people ask about REGEX

Why does my pattern only find the first match?

Because it has no g flag. Without g, JavaScript’s matching stops at the first result. Tick g in the flags row and the whole subject is scanned. With g the same RegExp object also carries a lastIndex between calls, which is the source of most surprising bugs when a pattern is reused in code.

Why does the dot not match my newline?

Because it never does by default. In JavaScript . means any character except a line terminator. Tick s (dotAll) and it matches newlines too. If you want the opposite, use an explicit class such as [^\n] so the intent is on the page rather than in a flag.

What is the difference between greedy and lazy?

a.*b takes as much as it can and then gives characters back until the pattern fits, so on a1b2b it matches through to the last b. a.*?b takes as little as it can and stops at the first b. Neither is right; they answer different questions. Switch the quantifier in the box above and watch the highlight move.

Is my pattern safe to run on untrusted input?

This page can tell you when a pattern backtracks badly on the subject you gave it, and it names the construct most likely to be responsible. It cannot prove a pattern is safe on every input, because that is undecidable in general. If a pattern runs against text a stranger supplies, prefer one with no nested quantifier over an overlapping class, and put a length limit on the input.

Does it support named groups and lookbehind?

Yes, because your browser does. (?<year>\d{4}) appears in the match list by name as well as by number, and (?<=\$)\d+ works in every current browser. Lookbehind arrived late in Safari, so a very old iOS will throw on it; the error is shown rather than swallowed.

Can I use this on a whole file?

Paste it in. The subject box will hold a few megabytes without complaint, and the match list is capped so a pattern matching a hundred thousand times does not build a hundred thousand rows of markup. The cap is stated when it is reached.

Why does \b not find a word boundary in café?

Because \w is exactly [A-Za-z0-9_], and \b is defined in terms of it. So a word boundary sits between caf and é: on café the pattern \bcafé\b fails and \bcaf\b succeeds. The u flag does not change this.

For text that is not ASCII, match on properties instead. \p{L} with u matches a letter, and (?<!\p{L}) and (?!\p{L}) do the job of the boundaries.

Is a lazy quantifier faster than a greedy one?

Not in general. It just fails in the other direction. <.*> runs to the end of the string and hands characters back until a > can be found; <.*?> takes as little as it can and tries again each time it is wrong.

The fastest of the three is usually neither. <[^>]*> can never overshoot, so it never has to come back.

Why does my validation pattern accept a second line?

Because the m flag is on. Without m, ^ and $ anchor to the start and end of the whole subject. With m (multiline) they anchor at every line break as well, so a second line of anything gets through.

JavaScript is stricter than Perl here. Without m, $ matches only at the very end and not before a trailing newline, so /^admin$/ rejects "admin\n". Turn m on and it accepts it.

Why does .test() return true and then false on the same string?

A RegExp with g or y keeps a lastIndex between calls. The second call starts where the first one stopped, finds nothing, and returns false. That is not a bug, it is the cursor moving.

Build the expression fresh, reset lastIndex, or use String.matchAll, which does it for you.

What is the difference between the y flag and the g flag?

Sticky matching (y) only succeeds if the match starts exactly at lastIndex. It never scans forward. That makes it the right tool for a tokeniser walking a string, and the wrong tool for finding something somewhere in it. g scans forward from lastIndex to find the next match.

What does the u flag change?

Without u, a pattern works on UTF-16 code units, so an emoji is two characters and . matches half of one. With u it works on code points, \p{...} properties become available, and unknown escapes such as \q become errors instead of meaning a literal q.

Turning u on can therefore break a pattern that was silently wrong before.

Why does the i flag not match some accented or non-Latin letters?

Case-insensitive is not the same as case-folded. i alone will not match the Kelvin sign against k, and will not fold the Turkish dotless ı the way a Turkish reader expects. With u the folding follows Unicode simple case folding, which is closer but still not the locale-aware answer. Normalise the text before matching when this matters.

Can a regular expression match nested brackets or HTML?

No. Nested brackets, HTML, and anything that has to count depth cannot be matched by a regular expression, however clever. JavaScript has no recursion and no atomic groups to soften this.

Where a pattern almost works on nested structure, it will be the malformed input that breaks it, which is exactly the input you were trying to catch.

Why is a library pattern loaded with the m flag?

The patterns in the library are the practical versions, not complete ones, and each says what it misses. A pattern anchored with ^ and $ is loaded with the m flag so every line of the sample is tested on its own. When you use it against a single value, drop the m and the g.

What can I write in the replacement box?

The same things JavaScript’s replace understands. $1 is a numbered group, $<name> a named group, $& the whole match, $` the text before it, $' the text after it, and $$ a literal dollar sign.

Doing this often? OBSCURA Umbra searches and replaces with regular expressions across every file in a folder. Everything is US$5 a month or US$48 a year.