Regular Expression

//g

Test String

How to Use the Regex Tester

Enter a regular expression pattern in the pattern field, toggle the flags you need, then paste your test string in the text area. Matches are highlighted in real time — each match shows its index position and any capture groups.

What is a Regular Expression?

A regular expression (regex) is a sequence of characters that defines a search pattern. It is used for string searching, validation, and text manipulation in virtually every programming language. Regex patterns can match literal text, character classes, repetitions, anchors, and more.

Common Use Cases

Regular expressions appear throughout software development, often in situations where simple string methods fall short. Understanding where regex genuinely helps — and where it adds unnecessary complexity — is half the skill.

  • Form validation — Email addresses, phone numbers, postal codes, and credit card numbers all follow structured formats that regex can verify client-side before a form is submitted. A pattern like ^\+?[\d\s\-().]15$ gives immediate feedback on whether a phone field looks plausible, reducing invalid submissions that reach your backend.
  • Log file parsing — Server logs, application traces, and access logs contain semi-structured text. Regex lets you extract timestamps, IP addresses, status codes, and error messages from lines that would be tedious to parse with fixed string splitting. A single pattern can handle slight format variations across log versions.
  • Search and replace in an IDE — Most code editors support regex find-and-replace. This lets you rename variables across a file, reformat dates from MM/DD/YYYY to YYYY-MM-DD, or strip trailing whitespace from specific line patterns — operations that would require dozens of manual edits otherwise.
  • Data extraction from HTML or plain text — When a full HTML parser is unavailable or overkill, regex can extract specific patterns such as all URLs in an anchor tag, all headings matching a prefix, or all numeric values in a structured report. Use this tool to test your extraction pattern against a real sample before deploying it. You can also compare output variations with our Diff Checker.
  • Input sanitization — Regex can strip or replace characters that are not expected in a given field — for example, removing non-digit characters from a phone number input before storing it, or normalizing whitespace in a name field. This is simpler than writing multiple conditional replacements.

Common Regex Patterns

  • Email[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}
  • URLhttps?://[^\s/$.?#].[^\s]*
  • IPv4 address(\d{1,3}\.){3}\d{1,3}
  • Date (YYYY-MM-DD)\d{4}-\d{2}-\d{2}
  • Hex color#([a-fA-F0-9]{6}|[a-fA-F0-9]{3})
  • Digits only^\d+$
  • Whitespace (any)\s+
  • Word boundary\bword\b

Regex Cheat Sheet

Anchors

  • ^ — Start of string (or line in multiline mode)
  • $ — End of string (or line in multiline mode)
  • \b — Word boundary
  • \B — Not a word boundary

Quantifiers

  • * — 0 or more
  • + — 1 or more
  • ? — 0 or 1 (also makes quantifiers lazy)
  • {n} — Exactly n times
  • {n,m} — Between n and m times

Character Classes

  • \d — Digit (0-9)
  • \w — Word character (a-z, A-Z, 0-9, _)
  • \s — Whitespace (space, tab, newline)
  • [abc] — Any of a, b, or c
  • [^abc] — Any character except a, b, or c

Groups

  • (abc) — Capture group
  • (?:abc) — Non-capturing group
  • (?<name>abc) — Named capture group
  • (?=abc) — Positive lookahead
  • (?!abc) — Negative lookahead

Regex Flags

  • g (global) — Find all matches instead of stopping after the first one.
  • i (case insensitive) — Match letters regardless of case.
  • m (multiline) — Make ^ and $ match the start and end of each line instead of the whole string.
  • s (dot all) — Make . match newline characters as well.

Debugging Tips

  • Start simple — Build your pattern incrementally. Match a literal string first, then add character classes and quantifiers.
  • Use non-greedy quantifiers — Replace .* with .*? to avoid over-matching.
  • Escape special characters — Characters like . * + ? ( ) [ ] { } ^ $ | \ must be escaped with \ to match literally.
  • Check your flags — Forgetting the g flag is the most common reason only the first match is returned.

Best Practices

Writing regex that works in a test case is straightforward. Writing regex that remains correct, readable, and performant six months later takes a bit more discipline.

  • Prefer named capture groups for readability — Instead of referencing match[1] and match[2], use named groups: (?<year>\d4)-(?<month>\d2). The result is self-documenting and does not break when you add or reorder groups later.
  • Avoid catastrophic backtracking — Nested quantifiers like (a+)+ or overlapping alternatives applied to long strings can cause the regex engine to explore an exponential number of paths. The symptom is a match that takes seconds or freezes the browser. Flatten nested quantifiers and make sure alternatives are mutually exclusive where possible.
  • Use non-capturing groups when you do not need the capture(?:foo|bar) groups alternatives without storing the match as a capture group. This keeps your group numbering clean and avoids allocating memory for results you will never read.
  • Compile and reuse regex in loops — In languages like Python and Java, creating a regex object inside a tight loop re-parses the pattern on every iteration. Store the compiled pattern in a variable outside the loop. In JavaScript, avoid new RegExp(...) inside hot paths for the same reason.
  • Add comments for complex patterns — The x (verbose or free-spacing) flag, available in Python, Ruby, and Perl, lets you add inline comments and whitespace to break a long pattern into readable segments. For JavaScript where the flag is not supported, document the pattern in a comment above it, explaining what each section matches.

Related Guides

FAQ

Is my data safe?

Yes. All regex testing happens entirely in your browser using JavaScript. No data is sent to any server.

Which flags are supported?

The tool supports g (global), i (case insensitive), m (multiline), and s (dot all) flags. You can combine multiple flags.

Why does my regex match unexpectedly?

Common pitfalls include forgetting to escape special characters like . * + ? ( ) [ ] { } ^ $ | \, or not using the global flag when you expect multiple matches.

What is a capture group?

A capture group is a part of the pattern wrapped in parentheses (). It lets you extract specific parts of a match. For example, (\d+)-(\d+) applied to 2024-01 captures 2024 and 01 as separate groups.

How do I match a literal dot?

Use \. instead of .. In regex, an unescaped dot matches any character. To match a literal period — for example in a file extension or IP address — you must escape it: \.txt or \d+\.\d+\.\d+\.\d+.

What is catastrophic backtracking?

Catastrophic backtracking occurs when a regex engine explores an exponentially large number of paths through a pattern before concluding there is no match. It is triggered by nested quantifiers such as (a+)+ applied to a string of repeated characters followed by a non-matching character. The engine tries every possible combination of how the inner and outer quantifiers can split the input, leading to execution times that grow exponentially with input length. The fix is to restructure the pattern so that alternatives are mutually exclusive and nested quantifiers cover non-overlapping character sets.

Can I use this tool for languages other than JavaScript?

The core regex syntax — character classes, quantifiers, anchors, and groups — works across most languages including Python, Java, Ruby, and PHP. However, some features are JavaScript-specific: named capture groups use (?<name>...) syntax which differs slightly in older engines, lookbehind assertions have limited support in some environments, and the s (dot all) flag is called re.DOTALL in Python. Use this tool to build and verify your pattern logic, then adapt the syntax to your target language if needed.

Related Articles