How to use the Regex Tester tool
- Write a patternEnter your regex between the slashes, or start from one of the “Common patterns”.
- Choose flagsg finds every match, i ignores case, m makes anchors work per line, s lets the dot cross line breaks, and u enables Unicode.
- Paste sample textAdd the text to search; each match is highlighted and listed with its position and groups.
- Preview a replaceEnter a replacement with $1, $2 or $<name>, then copy the output or the regex as JavaScript.
Regex tester features
- Live matchingHighlights and the results table refresh with every change to the pattern or sample text.
- Group inspectionEach match shows all of its numbered and named capture groups.
- Find and replaceCheck backreference replacements before you use them in code or an editor.
- Runaway protectionPatterns execute in a background worker and are halted after 1.5 seconds, so a bad regex can’t lock up the page.
- Ready-made patternsStarter expressions for emails, URLs, dates, phone numbers, IP addresses and more.
What is a regular expression?
A regular expression, or regex, is a compact pattern that describes a set of strings. It lets you search, validate and reshape text in ways plain search can’t, such as pulling every email address from a document, recognising dates in several formats, spotting repeated words or selecting lines that begin with a digit. Practically every programming language, code editor, database and spreadsheet supports regex.
Regex cheat sheet
Character classes
.stands for any single character other than a newline, or truly any character once the s flag is set.\d,\wand\smean a digit, a word character (letter, digit or underscore) and whitespace; capitalise them (\D,\W,\S) for the opposite.[xyz]picks one listed character,[^xyz]picks anything else, and[0-9]picks from a range.
Repetition
*means zero or more,+one or more,?optional.{4}is exactly four times,{1,3}one to three times,{2,}at least twice.- Adding
?after a quantifier, as in*?or+?, makes it lazy so it grabs as little as possible.
Positions and structure
^and$anchor to the beginning and end of the input, or of each line with m.\bmarks the edge of a word.( )captures,(?: )groups without capturing, and(?<year> )captures under a name.cat|dogmatches either alternative.(?= )and(?! )are lookaheads (must or must not follow);(?<= )and(?<! )are lookbehinds (must or must not precede).\1repeats whatever group 1 matched, known as a backreference.
Regex flags explained
- g, global: keep searching after the first hit.
- i, ignore case: treat “Hello” and “HELLO” alike.
- m, multiline: make ^ and $ apply to each individual line.
- s, dotAll: allow the dot to match newline characters.
- u, unicode: enable full Unicode handling, required for \p{L} (a letter in any script) and emoji.
Avoiding catastrophic backtracking
Certain patterns, (x+)+$ being the classic example, can take exponential time on input that nearly matches, hanging apps and even bringing down servers in what’s called a ReDoS attack. Steer clear of nested quantifiers over the same characters, favour precise classes such as [^"]* over .*, and anchor patterns where possible. This tester halts anything that runs longer than 1.5 seconds, so you can find the culprit safely.
Regex flavours
The tester uses your browser’s JavaScript (ECMAScript) regex engine. Python, PHP (PCRE), Java and .NET share most of the syntax but differ in details: JavaScript writes $1 in replacements where Python writes \1, for example, and some engines offer possessive quantifiers JavaScript lacks. To see precisely what a replacement altered, compare before and after in the Text Diff Checker, or apply patterns to whole documents with Find and Replace.