Regex Tester

Test and debug regular expressions in real time with match highlighting, capture group inspection, flag toggles, and a built-in syntax cheat sheet.

//g
hello 123 world

What are Regular Expressions & How Do Engines Evaluate Them?

A Regular Expression (commonly abbreviated as Regex or RegExp) is a formal language sequence of characters defining a search pattern. Rooted in theoretical computer science and formal automata theory, regular expressions are used across all major programming languages for string validation, lexical analysis, data extraction, and find-and-replace operations.

Modern programming languages implement regex evaluation through Nondeterministic Finite Automaton (NFA) or Deterministic Finite Automaton (DFA) engines. JavaScript (V8), Python, Java, and PCRE use backtracking NFA engines, which support powerful features like lookarounds, non-greedy matches, and numbered backreferences, but require careful design to avoid catastrophic performance bottlenecks.

The Essential Regex Flags Decoded

Regex modifiers (flags) alter how the engine interprets text boundaries and character matching:

  • g (Global Match): Finds all matches throughout the target string rather than halting at the first match.
  • i (Ignore Case): Performs case-insensitive matching (treating [a-z] and [A-Z] as identical).
  • m (Multiline): Modifies the caret (^) and dollar sign ($) anchors to match the start and end of each individual line (delimited by \n), rather than only the beginning and end of the entire input document.
  • s (DotAll): Allows the wildcard dot (.) to match newline characters (\n and \r), which it normally excludes.
  • u (Unicode): Enables full UTF-16 surrogate pair decoding and Unicode property escapes (such as \p{Emoji} or \p{Script=Greek}).

Lookaround Assertions: Zero-Width Pattern Matching

Lookarounds are zero-width assertions: they match characters based on what precedes or follows them without including those characters in the matched output string:

  • Positive Lookahead ((?=...)): Asserts that the specified pattern follows. For example, \d+(?=px) matches the digits 100 in "100px" without including"px" in the match.
  • Negative Lookahead ((?!...)): Asserts that the specified pattern does not follow. For example, \d+(?!px) matches numbers that are not followed by pixel units.
  • Positive Lookbehind ((?<=...)): Asserts that the specified pattern precedes the current match. For example, (?<=\$)\d+ matches 50 in "$50" without capturing the dollar sign.
  • Negative Lookbehind ((?<!...)): Asserts that the specified pattern does not precede the match.

Greedy vs. Lazy Quantifiers & Preventing Catastrophic ReDoS

Understanding quantifier mechanics is essential to prevent system lockups and security vulnerabilities:

  • Greedy Quantifiers (*, +): By default, quantifiers consume as much text as possible. In <b>Hello</b> <b>World</b>, the pattern <b>.*</b> matches all the way to the final closing tag.
  • Lazy Quantifiers (*?, +?): Appending a question mark forces the quantifier to match as little as possible.<b>.*?</b> correctly matches each bold tag individually.
  • Regular Expression Denial of Service (ReDoS): Nested repeating quantifiers with overlapping alternatives—such as (a+)+$ or ([a-zA-Z0-9]+)*=—force NFA engines into exponential backtracking when matched against long non-matching inputs (e.g., "aaaaaaaaaaaaaaaaaaaa!"). The engine tests 2^n combination permutations, causing 100% CPU spikes and freezing single-threaded Node.js event loops. Always avoid nesting quantifiers inside groups that are themselves quantified.

Tested Production Patterns & Practical Recipes

Here are battle-tested regex recipes commonly used in production applications:

  • Pragmatic Email Validation: ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ (balances RFC compliance with practical web validation).
  • ISO 8601 YYYY-MM-DD Date: ^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])$
  • Semantic Versioning (SemVer 2.0.0): ^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-([0-9A-Za-z.-]+))?(?:\+([0-9A-Za-z.-]+))?$
  • Clean URL Slug: ^[a-z0-9]+(?:-[a-z0-9]+)*$ (strictly enforces lowercase alphanumerics separated by single hyphens).

Practical Real-World Use Cases

  • Extract identifiers from log lines: Test capture groups against sample server logs to reliably pull out request IDs, IP addresses, or error codes.
  • Validate custom input formats: Build and verify patterns for structured strings like SKU codes, internal ticket numbers, or semantic version tags.
  • Locate date or timestamp patterns: Test expressions that match varying date formats across unstructured text reports before writing a parser.
  • Preview search-and-replace rules: Verify exactly which substrings a pattern captures before executing a bulk find-and-replace across a codebase.
  • Isolate edge-case failures: Test a regular expression against valid strings, invalid inputs, empty values, and boundary cases to fix greedy matching or false positives.

Absolute Privacy

Your text is safe. Testing regular expressions against sensitive strings (such as production server logs, auth tokens, SQL queries, or confidential database exports) can be dangerous on server-backed tools. OmniTools executes all regular expressions natively in your web browser using JavaScript's built-inRegExp engine.

Your input text and regex patterns are never uploaded to any backend servers or recorded in analytics databases.

Frequently Asked Questions

How do I test and validate a regular expression online?

Enter your regex pattern and flags (g, i, m, s, u) above and paste your test string. OmniTools highlights all matches, capture groups, and character positions in real time using the native browser RegExp engine.

What is the difference between greedy and lazy regex quantifiers?

Greedy quantifiers (*, +) match as many characters as possible before backtracking, whereas lazy quantifiers (*?, +?) match the shortest possible substring that satisfies the pattern.