Skip to content

Regex Tester and Validator - Test Patterns Online

Test regular expressions online. This free tool checks regex syntax, highlights matches in real time, and shows whether a pattern matches the whole text.

Regex Pattern Tester

Match Results

Enter a pattern and test text to see results

Save Pattern

Saved Patterns

No saved patterns yet

Regex Symbols Guide

.Matches any character except newline
\dMatches any digit (0-9)
\DMatches any non-digit
\wMatches any word character (a-z, A-Z, 0-9, _)
\WMatches any non-word character
\sMatches any whitespace character
\SMatches any non-whitespace character
^Matches start of line
$Matches end of line
*Matches 0 or more of the preceding character
+Matches 1 or more of the preceding character
?Matches 0 or 1 of the preceding character
{n}Matches exactly n of the preceding character
{n,}Matches at least n of the preceding character
{n,m}Matches between n and m of the preceding character
[abc]Matches any one of the characters in the brackets
[^abc]Matches any character not in the brackets
(abc)Groups multiple tokens together and captures the match
a|bMatches either a or b
\bMatches a word boundary position
Loading calculator...
📚

Documentation

What is a regex tester

A regex tester is a tool that checks a regular expression against a piece of text and shows what it matches. A regular expression, or regex, is a short sequence of characters that describes a pattern in text, such as "three digits in a row" or "any word starting with a capital letter." This regex validator lets a person type a pattern and some test text, then see the matches highlighted right away, without writing any code.

Regular expressions are used in many programming languages to search text, check that user input is in the right format, and pull specific pieces out of a larger block of text. The syntax is compact but easy to get wrong, so testing a pattern before using it in a program helps catch mistakes early.

How to test a regular expression online

  1. Type a pattern. Enter the regex in the pattern field. The tool checks the syntax as the user types and shows an error message if the pattern is invalid, for example if a bracket or parenthesis is left open.
  2. Choose flags. Flags change how the pattern is applied (see below).
  3. Enter test text. Type or paste the text to search in the test text box.
  4. Read the results. Matches are highlighted in the text, and the tool reports how many matches it found. If there is test text, it also states whether the pattern matches the whole text from start to end, or only part of it.
  5. Save a pattern. A pattern can be stored with a short label so it can be reused later. Saved patterns are kept in the browser, not on a server, so they only appear on the same device and browser.
  6. Copy results. The matched text or the pattern itself can be copied to the clipboard with one click.

Regex flags

A flag is a single letter added after a pattern that changes how it behaves. This tool supports four flags, alone or combined:

FlagNameEffect
gGlobalFinds every match in the text instead of stopping at the first one.
iCase insensitiveIgnores uppercase and lowercase differences.
mMultilineMakes ^ and $ match the start and end of each line, not just the start and end of the whole text.
sDot allMakes . also match newline characters.

Flags can be combined, for example gi for global and case-insensitive, or gms for global, multiline, and dot-all together.

How the tool finds matches

The matcher always searches for every occurrence of the pattern in the text, which is why match counting behaves as if the global flag is on even when it is not selected in the dropdown. Each match is recorded with its position and length, then the original text is split into matched and non-matched pieces so the matches can be highlighted.

To check whether a pattern matches the entire text, the tool wraps the pattern in a non-capturing group and anchors it: ^(?:pattern)$. The grouping matters because | (alternation) binds more loosely than the anchors. Without the group, a pattern like cat|dog anchored as ^cat|dog$ would really mean "starts with cat, or ends with dog," which is not the same as "the whole text is cat or dog."

Worked example

Pattern: \d+ (one or more digits), flag: g, test text: Room 12 has 5 chairs.

  • Matches found: 12 and 5, so the match count is 2.
  • Whole-text check: the pattern does not match the entire text, because the text also contains letters and spaces.

If the test text is changed to 12345 instead, the same pattern matches the whole string, so the whole-text check reports true.

Regex syntax reference

Character matching

SymbolMeaningExampleMatches
.Any character except a newlinea.c"abc", "adc", "a1c"
\dAny digit, 0 to 9\d{3}"123", "456"
\DAny character that is not a digit\D+"abc", "xyz"
\wAny letter, digit, or underscore\w+"abc123", "test_123"
\WAny character that is not a letter, digit, or underscore\W+"!@#"
\sAny whitespace charactera\sb"a b"
\SAny character that is not whitespace\S+"abc"

Position anchors

SymbolMeaningExampleMatches
^Start of a line^abc"abc" at the start of a line
$End of a lineabc$"abc" at the end of a line
\bA word boundary\bword\b"word" as a whole word

Quantifiers

SymbolMeaningExampleMatches
*Zero or more of the previous itema*b"b", "ab", "aab"
+One or more of the previous itema+b"ab", "aab"
?Zero or one of the previous itemcolou?r"color", "colour"
{n}Exactly n of the previous itema{3}"aaa"
{n,}At least n of the previous itema{2,}"aa", "aaa"
{n,m}Between n and m of the previous itema{2,4}"aa", "aaa", "aaaa"

Character sets and groups

SymbolMeaningExampleMatches
[abc]Any one character listed in the brackets[aeiou]any single vowel
[^abc]Any character not listed in the brackets[^aeiou]any non-vowel character
(abc)Groups characters and captures the match(abc)+"abc", "abcabc"
a|bMatches a or bcat|dog"cat", "dog"

Two more features are worth knowing. A lookahead, written a(?=b), matches "a" only when it is followed by "b" without including "b" in the match. A lookbehind, written (?<=a)b, matches "b" only when it comes right after "a".

Common pattern examples

These patterns can be pasted into the tool to see how they behave against sample text.

Digits only: ^\d+$ matches a string made entirely of digits, such as "48210", and rejects anything with a letter or space.

Email address (basic check): ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ matches addresses in the form name@domain.tld, such as "user@example.com". It is a simple check, not the full email format defined by internet standards, so some unusual but valid addresses will fail it.

US phone number: ^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$ matches formats like "(123) 456-7890", "123-456-7890", and "1234567890".

Date in YYYY-MM-DD format: ^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$ checks that the month is between 01 and 12 and the day is between 01 and 31. It does not check that the day is valid for the specific month, so it accepts "2024-02-30" even though February never has 30 days.

Frequently asked questions

What is a regular expression? A regular expression is a sequence of characters that defines a search pattern. It is used to search, match, and edit text in most programming languages and text editors.

What does the global flag do? Without the global flag, a search normally stops after the first match. With the global flag (g), the search continues and reports every match in the text. This tool always searches for all matches when counting them, regardless of the flag chosen.

How do I match a literal dot or other special character? Put a backslash before it. For example, \. matches a literal dot instead of "any character," which is what an unescaped . means.

What is the difference between .* and .*?? .* is greedy and matches as many characters as it can. .*? is lazy and matches as few characters as it can while still letting the rest of the pattern succeed.

How do I check that a whole string matches a pattern, not just part of it? Add ^ at the start and $ at the end of the pattern, for example ^[0-9]+$ for a string of only digits.

Does this tester match the regex rules of other programming languages? This tool uses the regular expression engine built into JavaScript. Most core syntax, such as character classes and quantifiers, is shared across languages including Python, Java, and Perl, but some details differ, so a pattern that works here is not guaranteed to behave identically everywhere.

References

  1. "Regular expression." Wikipedia, Wikimedia Foundation. https://en.wikipedia.org/wiki/Regular_expression
  2. MDN Web Docs. "Regular expressions." Mozilla. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
  3. Friedl, J. E. F. (2006). Mastering Regular Expressions. O'Reilly Media.