Regex Tester

Build, test, and explain regular expressions

Regex Tester

Supports capture groups: $1, $2, $& (whole), $` (before), $' (after), and named $<name>.
RegExr was created by gskinner.com.

Edit the Expression & Text to see matches. Roll over matches or the expression for details. PCRE & JavaScript flavors of RegEx are supported. Validate your expression with Tests mode.

The side bar includes a Cheatsheet, full Reference, and Help. You can also Save & Share with the Community and view patterns you create or favorite in My Patterns.

Explore results with the Tools below. Replace & List output custom results. Details lists capture groups. Explain describes your expression in plain English.

How to use the Regex Tester

  1. Enter your regular expression in the pattern field at the top. The tool supports standard JavaScript regex syntax.
  2. Set flags such as g (global), i (case-insensitive), m (multiline), s (dotAll), and u (Unicode) using the flag toggles.
  3. Provide test text in the text area below. Matches are highlighted in real time as you type both the pattern and the test string.
  4. Review match details including captured groups, match positions, and the full match information displayed below the text area.
  5. Test replacements by entering a replacement string to see what the text looks like after applying the regex substitution.

What are regular expressions?

Regular expressions (regex or regexp) are patterns that describe sets of strings. They provide a powerful and concise way to search, match, and manipulate text. Originally developed from formal language theory in the 1950s, regular expressions are now supported in virtually every programming language and text editor.

A regex pattern is composed of literal characters and special metacharacters. Literal characters match themselves: the pattern cat matches the text "cat". Metacharacters have special meanings:

  • . matches any single character (except newline by default).
  • *, +, ? are quantifiers: zero or more, one or more, and zero or one, respectively.
  • [abc] is a character class matching any one of the listed characters.
  • [^abc] is a negated character class matching any character NOT listed.
  • \d, \w, \s are shorthand classes for digits, word characters, and whitespace.
  • ^ and $ are anchors matching the start and end of the string (or line, with the m flag).
  • () creates capturing groups for extracting parts of the match.
  • | is alternation, matching the pattern on either side.

Quantifiers are greedy by default (they match as much text as possible). Adding ? after a quantifier makes it lazy (matching as little as possible). For example, .*? matches the shortest possible string, which is important when matching between delimiters like HTML tags.

Lookaheads ((?=...) and (?!...)) and lookbehinds ((?<=...) and (?<!...)) assert that certain text exists (or does not exist) before or after the current position without including it in the match. These zero-width assertions are powerful for complex pattern matching.

Common use cases

  • Input validation: Validate email addresses, phone numbers, postal codes, dates, and other formatted strings using patterns that define the expected structure.
  • Search and replace: Find and replace text patterns in code editors and command-line tools. Regex-powered search is far more powerful than literal text search.
  • Log parsing: Extract timestamps, error codes, IP addresses, and other structured data from log files using capture groups.
  • Data extraction and scraping: Pull specific data out of HTML, CSV, or other text formats using patterns that match the data's structure.
  • Code refactoring: Rename variables, restructure function calls, or update API patterns across an entire codebase using regex find-and-replace.

FAQ

Why is my regex matching more than I expected? The most common cause is greedy quantifiers. .* matches as much text as possible. If you want to match the shortest string, use the lazy version .*?. Also check that you are anchoring your pattern with ^ and $ if you want to match the entire string.

What is the difference between * and +? * matches zero or more occurrences (the preceding element is optional and can repeat). + matches one or more occurrences (the preceding element must appear at least once). For example, a* matches "", "a", "aa", while a+ matches "a" and "aa" but not "".

How do I match a literal dot or other special character? Escape it with a backslash: \. matches a literal period, \* matches a literal asterisk, and \\ matches a literal backslash. Inside a character class [], most metacharacters lose their special meaning except ], \, ^, and -.

Is my data safe?

Yes. This tool runs entirely in your browser. Your data is never sent to our servers. Regex matching is performed using JavaScript's native RegExp engine running locally on your device.

How to use the Regex Tester

  1. Enter your regular expression in the pattern field at the top. The tool supports standard JavaScript regex syntax.
  2. Set flags such as g (global), i (case-insensitive), m (multiline), s (dotAll), and u (Unicode) using the flag toggles.
  3. Provide test text in the text area below. Matches are highlighted in real time as you type both the pattern and the test string.
  4. Review match details including captured groups, match positions, and the full match information displayed below the text area.
  5. Test replacements by entering a replacement string to see what the text looks like after applying the regex substitution.

What are regular expressions?

Regular expressions (regex or regexp) are patterns that describe sets of strings. They provide a powerful and concise way to search, match, and manipulate text. Originally developed from formal language theory in the 1950s, regular expressions are now supported in virtually every programming language and text editor.

A regex pattern is composed of literal characters and special metacharacters. Literal characters match themselves: the pattern cat matches the text "cat". Metacharacters have special meanings:

  • . matches any single character (except newline by default).
  • *, +, ? are quantifiers: zero or more, one or more, and zero or one, respectively.
  • [abc] is a character class matching any one of the listed characters.
  • [^abc] is a negated character class matching any character NOT listed.
  • \d, \w, \s are shorthand classes for digits, word characters, and whitespace.
  • ^ and $ are anchors matching the start and end of the string (or line, with the m flag).
  • () creates capturing groups for extracting parts of the match.
  • | is alternation, matching the pattern on either side.

Quantifiers are greedy by default (they match as much text as possible). Adding ? after a quantifier makes it lazy (matching as little as possible). For example, .*? matches the shortest possible string, which is important when matching between delimiters like HTML tags.

Lookaheads ((?=...) and (?!...)) and lookbehinds ((?<=...) and (?<!...)) assert that certain text exists (or does not exist) before or after the current position without including it in the match. These zero-width assertions are powerful for complex pattern matching.

Common use cases

  • Input validation: Validate email addresses, phone numbers, postal codes, dates, and other formatted strings using patterns that define the expected structure.
  • Search and replace: Find and replace text patterns in code editors and command-line tools. Regex-powered search is far more powerful than literal text search.
  • Log parsing: Extract timestamps, error codes, IP addresses, and other structured data from log files using capture groups.
  • Data extraction and scraping: Pull specific data out of HTML, CSV, or other text formats using patterns that match the data's structure.
  • Code refactoring: Rename variables, restructure function calls, or update API patterns across an entire codebase using regex find-and-replace.

FAQ

Why is my regex matching more than I expected? The most common cause is greedy quantifiers. .* matches as much text as possible. If you want to match the shortest string, use the lazy version .*?. Also check that you are anchoring your pattern with ^ and $ if you want to match the entire string.

What is the difference between * and +? * matches zero or more occurrences (the preceding element is optional and can repeat). + matches one or more occurrences (the preceding element must appear at least once). For example, a* matches "", "a", "aa", while a+ matches "a" and "aa" but not "".

How do I match a literal dot or other special character? Escape it with a backslash: \. matches a literal period, \* matches a literal asterisk, and \\ matches a literal backslash. Inside a character class [], most metacharacters lose their special meaning except ], \, ^, and -.

Is my data safe?

Yes. This tool runs entirely in your browser. Your data is never sent to our servers. Regex matching is performed using JavaScript's native RegExp engine running locally on your device.