Regular Expression Tester

Formula: new RegExp(pattern, flags).match(text)

Regex Tester

Regular expressions (regex) are patterns used to search, match, and manipulate text. They are supported in every major programming language and are invaluable for data validation, parsing, and transformation.

Conversion Formula

new RegExp(pattern, flags).match(text)

The pattern is compiled into a RegExp object with the specified flags. The g (global) flag is always added to find all matches. The test text is matched against the pattern and each match is highlighted.

Step-by-Step Examples

Pattern: \d+, Text: "Order 42, Item 7, Cost 199" = 3 matches: 42, 7, 199

\d+ matches one or more consecutive digits

Pattern: [A-Z][a-z]+, Flags: g, Text: "Hello World" = 2 matches: Hello, World

Matches capitalized words

History

Regular expressions originate from formal language theory (Kleene, 1956). They were popularized in Unix tools like grep and sed (1970s), then adopted by Perl (1987) which shaped the modern PCRE standard used in most languages today.

Common Use Cases

  • Form input validation
  • Log file parsing
  • Search and replace
  • Data extraction from text
  • URL routing
  • Syntax highlighting

Frequently Asked Questions

What do regex flags do?

g = global (find all matches, not just first), i = case-insensitive, m = multiline (^ and $ match line boundaries), s = dotAll (. matches newlines), u = unicode mode.

What are some commonly useful regex patterns?

Email: [\w.-]+@[\w.-]+\.[a-z]{2,} | URL: https?://[^\s]+ | Phone: \+?[\d\s()-]{7,15} | Numbers: \d+(\.\d+)? | HTML tag: <[^>]+>

What is a capturing group?

Parentheses () create a capturing group that lets you extract specific parts of a match. For example, (\d{4})-(\d{2})-(\d{2}) captures year, month, and day from a date.

Why is my regex slow or hanging?

Catastrophic backtracking can freeze regex engines. Avoid nested quantifiers like (a+)+ on long strings. Use possessive quantifiers or atomic groups if your engine supports them.