Regex, Quick Facts
- •A regular expression (regex) is a pattern used to match, extract, or validate text.
- •The g flag finds every match instead of stopping at the first one; i ignores case.
- •Common tokens:
\ddigit,\wword character,\swhitespace,+one or more,*zero or more,?optional. - •Parentheses
()create a capture group you can extract separately from the full match.
The Real Problem This Solves
Writing a regex that looks right, and a regex that actually matches every real-world input, an email with a plus sign, a phone number with spaces, a log line with an unexpected character, are two very different things.
Testing against real strings before you ship the pattern into validation or a script catches the edge cases a "looks correct" pattern misses.
How Pattern Matching Works
A regex engine scans your text left to right, trying the pattern at every starting position until it finds a match, or, with the g flag, keeps going and collects every match in the string. Capture groups, the parts inside parentheses, are pulled out separately so you can extract just the piece you need, like the domain from an email address.
Example: The pattern [\w.+-]+@[\w-]+\.[a-zA-Z]{2,} matches support@calculatorwala.in as a whole, and correctly skips a bare word like "contact" that has no @ symbol, because every part of the pattern before the @ must be satisfied first.
| Token | Meaning |
|---|---|
. | Any character except a newline |
\d, \w, \s | Digit, word character, whitespace |
+, *, ? | One or more, zero or more, zero or one |
^, $ | Start of string/line, end of string/line |
(...) | Capture group |
(?:...) | Non-capturing group |
Frequently Asked Questions
What does the g flag do?
Without it, the engine stops after the first match. With the g flag, it finds every match in the string, which is what most validation and extraction tasks actually need.
Why isn't my pattern matching multi-line text?
By default, ^ and $ match only the very start and end of the whole string. Add the m flag to make them match the start and end of each individual line instead.
What is the difference between .* and .+?
.* matches zero or more characters, so it can match an empty string. .+ requires at least one character to be present to match.
How do I make part of a pattern optional?
Add a ? right after it. For example, colou?r matches both "color" and "colour" because the u becomes optional.
Is this JavaScript regex or PCRE?
This tool uses your browser's native JavaScript regex engine (ECMAScript), the same one behind String.match(), .replace(), and .test() in any JavaScript codebase. Behaviour can differ slightly from Python's re or PHP's PCRE.
Pulled data out with a capture group?
If what you're really validating or reshaping is a JSON payload, format and inspect it cleanly with our JSON Formatter.
Open JSON Formatter →