Regex: how to test regular expressions without the headache

Learn how to test regular expressions, understand flags, read capture groups, and debug patterns with real-time match highlighting.

Regular expressions — or regex — are a language for describing text patterns. They let you find emails in a log, validate a date format, extract numbers from a spreadsheet, or replace repeated passages without dozens of manual searches.

The catch is that regex has a reputation for being unreadable. This guide covers the essential syntax, the most-used flags, practical examples, and how to debug a pattern without guessing. To test while you read, use the regex tester.

What is a regular expression?

A regex is a pattern that the search engine compares against text. Each symbol has a meaning: \d stands for a digit, . stands for any character, * means repetition. Together they describe the format you want to find.

The tester on this site uses JavaScript's native engine (RegExp). That matters: regex is not one single language. Features available in JavaScript may not exist — or may behave differently — in Python, PHP or PCRE. A pattern that works in one place can fail in another.

Essential syntax

PatternMeaning
.Any character except a line break (without the s flag)
\d / \DDigit / non-digit
\w / \WLetter, number or underscore / the opposite
\s / \SWhitespace / non-whitespace
^ / $Start / end of text (or line, with m)
* / + / ?Zero or more / one or more / zero or one
{n} / {n,m}Exactly n / between n and m repetitions
[abc] / [^abc]One character from the list / one character outside it
(...)Capture group
(?:...)Non-capturing group
|Alternation: “this or that”
\Escapes a special character

To match a literal dot, escape it: \.. The same applies to parentheses, brackets, braces, +, *, ?, |, ^ and $.

In JavaScript, \w covers ASCII letters, digits and underscore. For accented letters, use \p{L} with the u flag.

Flags: changing how the search behaves

The g flag is checked by default in the tester. Without it, the tool shows only the first occurrence — which often confuses people expecting every result.

Practical examples

Finding emails

A simple pattern for emails in running text:

\b[\w.]+@[\w.]+\.\w+\b

It covers common formats, but it does not validate every case allowed by the email specification. For sign-up forms, use a basic client-side check and confirm on the server — regex alone cannot prove the address exists.

Dates in dd/mm/yyyy format

\b\d{2}/\d{2}/\d{4}\b

This finds the format, but it does not validate whether the date exists (31/02 would pass). To validate properly, combine regex with a date check.

Extracting data with groups

(\d{2})/(\d{2})/(\d{4})

The parentheses capture day, month and year separately. The tester shows each numbered group; if the pattern uses named groups such as (?<year>\d{4}), the name also appears in the result.

Trimming whitespace

^\s+|\s+$

The | works as “or”: whitespace at the start or at the end. In code, many languages already provide a trim() function; regex is useful when you need the pattern inside a search tool.

How to test and debug a pattern

  1. Start with a small sample of text that contains what you want to find.
  2. Build the pattern in parts: first the fixed text, then the groups and quantifiers.
  3. Enable the g flag to see every occurrence.
  4. Check the highlight and the match list, including the index of each occurrence.
  5. Add edge cases: no match, accents, line breaks, special characters.
  6. If the pattern becomes complex, simplify it. Hard-to-read regex is hard to maintain.

To test the pattern against varied data, the random data generator creates fake emails, phone numbers, names and dates.

Common mistakes

Test your pattern now

In the regex tester, paste the pattern and the text, choose the flags, and see highlighted matches, the position of each occurrence and the capture groups in real time. Everything runs in the browser with JavaScript's native engine — the pattern and the text are not sent to any server.