About the Regex Tester
Regular expressions are powerful but notoriously easy to get wrong. This regex tester highlights every match in your sample text as you type the pattern, lists capture groups for each match and lets you switch flags such as global, case-insensitive and multiline. A pattern such as \d{4}-\d{2}-\d{2}, for example, finds dates written in the international format. It uses the JavaScript regex engine, which most web projects rely on, so results match your code. For replacing text with a pattern, use Find and Replace, and to extract specific items, try the Email Extractor or URL Extractor.
Beneath the coloured sample sits a table of hits, giving the offset where each begins and the text its groups picked up. Parentheses define those groups, and they are where the value lies: confirming that a date is present is mildly useful, while pulling out its year, month and day individually lets you rebuild it in another order.
Every standard JavaScript flag is available as a switch: g to find all matches instead of only the first, i to ignore case, m so the start and end anchors work line by line, s so a dot also matches line breaks, u for full Unicode support and y for sticky matching. A broken pattern produces the browser's own error message instead of failing silently, and a short reference of common tokens sits beside the tester for when you cannot recall whether you need \d or \w.
How to use the Regex Tester
- Type the patternEnter the regular expression on its own, without the slashes around it.
- Choose the flagsTurn on global, ignore case, multiline and the others with the switches next to the pattern.
- Add sample textMatches are highlighted as you edit either the pattern or the text.
- Study the resultsEvery hit appears in a table showing where it starts and what each group captured.
Regex Tester features
- Matches highlighted in the sample as you type
- Switches for all the standard flags
- List of matches with their positions and capture groups
- Named groups displayed under their names
- Clear explanation when a pattern is invalid
- Protection against patterns that endlessly match nothing
- A compact cheat sheet of common tokens
Common uses
- Writing the validation rule for a form field
- Pulling values out of log lines or copied web text
- Checking what a pattern in a pull request really matches
- Learning regular expressions on text you already know
- Perfecting a pattern before using it in Find and Replace
Regular expressions in practice: Pakistani phone numbers, CNIC formats, extraction, performance and readability
Regular expressions are compact patterns for finding and checking text, used for validating forms, cleaning data and searching logs. They are powerful and easy to get subtly wrong, so testing them against real examples is essential. This guide builds patterns for Pakistani mobile numbers and CNIC numbers, explains greedy and lazy matching, warns about patterns that can freeze a program and offers advice on readable, maintainable expressions.
A worked example: mobile numbers
Pakistani mobile numbers are often written as 03001234567, 0300-1234567 or +923001234567. One pattern that accepts these forms is:
^(?:\+92|0)3\d{2}-?\d{7}$
It requires either +92 or 0 at the start, then 3 and two more digits for the network code, an optional hyphen and seven digits. Testing it against valid numbers and deliberately wrong ones, such as numbers with too few digits or a landline code, confirms it accepts and rejects the right things.
A worked example: CNIC numbers
Pakistani CNIC numbers are 13 digits, usually written with hyphens as five digits, seven digits and one digit. The pattern ^\d{5}-\d{7}-\d$ checks this layout. It confirms the format only, not whether a number really exists, which requires checking with the issuing authority.
Extracting data
Regular expressions also extract information from text. A pattern such as ORD-\d{6} finds order numbers like ORD-104233 in emails or chat logs, and capture groups pull out the parts you need. Testing against a varied sample of real text reveals cases the pattern misses, such as lower-case prefixes.
Greedy and lazy matching
Quantifiers such as .* are greedy by default, matching as much as possible. Applied to an HTML snippet, <b>.*</b> can swallow everything from the first opening tag to the last closing tag. The lazy version, .*?, stops at the first possible match. Many surprising results come from greedy matching where lazy was intended.
Anchors and escaping
Without the anchors ^ and $, a validation pattern may match part of a longer string, accepting input that contains extra characters. Special characters such as the dot, plus sign and brackets must be escaped with a backslash when they should match literally, so matching a full stop needs \. rather than ..
Patterns that freeze programs
Some patterns with nested repetition, such as (a+)+, can take an enormous time on certain inputs, a problem known as catastrophic backtracking. On a server, an attacker can exploit this to slow or stop the service, an attack called regular expression denial of service. Keeping patterns simple, avoiding nested quantifiers and limiting input length reduce the risk.
Readability
Regular expressions are hard to read months later. Breaking complex patterns into named parts, adding comments where the language supports a verbose mode and keeping a list of test examples alongside the pattern make maintenance much easier. A short, slightly less strict pattern is often better than an unreadable perfect one.
When not to use regex
Regular expressions are poor tools for parsing nested structures such as HTML and JSON, and fully validating email addresses with a single pattern is notoriously complex. Proper parsers, or simple checks followed by a confirmation email, usually work better.
Different flavours
JavaScript, Python, PHP and other languages support slightly different regex features and syntax. A pattern tested here uses the JavaScript flavour, so check features such as lookbehind and named groups when moving a pattern to another language.
Common mistakes
- Forgetting anchors in validation patterns.
- Using greedy
.*where a lazy match was intended. - Not escaping dots, plus signs and brackets.
- Writing nested quantifiers that can backtrack catastrophically.
Related tools
To clean text before matching, use Text Cleaner. To find and replace across text, try Find and Replace, and to compare text before and after, Text Diff Checker.
Character classes and Urdu text
In JavaScript, \w matches only English letters, digits and the underscore, so it does not match Urdu letters. With the Unicode flag, property escapes such as \p{L} for any letter, or \p{Script=Arabic} for the Arabic script used by Urdu, match these characters properly. Testing patterns with real Urdu names shows quickly whether a form would wrongly reject them.
Urdu digits on phone keyboards
Some phone keyboards set to Urdu produce Extended Arabic-Indic digits, such as ۰۳۰۰, instead of 0300. The class \d in JavaScript matches only the ASCII digits 0 to 9, so a valid number typed this way fails validation. Converting these digits to ASCII before checking, or accepting them explicitly in the pattern, prevents frustrated users and lost sign-ups.
Building patterns step by step
Start with a literal example that matches one real value, then replace parts with character classes, add quantifiers and finally add anchors, testing after each change. Building gradually shows exactly which step breaks a match. Trimming spaces and removing hyphens from input before matching also keeps patterns shorter and easier to understand for whoever maintains them next.
Reformatting with replacements
Capture groups can rearrange text as well as find it. The pattern (\d{2})/(\d{2})/(\d{4}) with the replacement $3-$2-$1 turns 25/10/2026 into 2026-10-25, a format that sorts correctly. Testing replacements on a sample before running them across a whole file or database avoids changes that are hard to undo.
Privacy
Patterns are tested in your browser. Your sample text is not uploaded or stored.
Frequently asked questions
Which regex dialect does the tester use?
JavaScript's, because your browser's own engine does the matching. That covers most common syntax, but a few features from other languages, such as possessive quantifiers, or lookbehind in older browsers, may behave differently or be unavailable.
Why is only the first match found?
The global flag is switched off. Without it, the engine stops as soon as it finds one match, so turn on g to highlight and list every occurrence in the sample text.
How do the multiline and dot-all flags differ?
With multiline on, the caret and dollar anchors apply to every individual line instead of only to the very beginning and end of the input. Dot-all lets the dot match line-break characters as well. They solve different problems and are easy to mix up.
What are capture groups, and why name them?
Brackets in a pattern capture whatever they matched, so the value can be reused afterwards. Giving a group a name with the ?<name> syntax makes the result far easier to read, especially once a pattern has several groups.
Why does the tool warn that a pattern matches an empty string?
If a pattern can succeed without consuming any characters, a global search would get stuck repeating the same zero-length hit. The tester detects this and halts, keeping the page responsive while you fix the pattern.
Is my sample text uploaded anywhere?
No. All matching happens in your browser, so log excerpts, customer data and other real text you test against stay on your own computer and are never sent to a server.