The building blocks
| Pattern | Meaning | Example match |
|---|---|---|
| . | Any character except a line break | a.c matches abc |
| \d \w \s | A digit, a word character, a whitespace | \d matches 7 |
| [abc] [a-z] | One character from a set or range | [a-c] matches b |
| [^abc] | Any character not in the set | [^0-9] matches x |
| * + ? | 0 or more, 1 or more, 0 or 1 | colou?r matches color and colour |
| {3} {2,4} | Exactly 3, or between 2 and 4 times | \d{3} matches 555 |
| ^ $ | Start and end of the text | ^Hello matches only at the start |
| | | Either this or that | cat|dog |
| ( ) | A group (captured for reuse) | (\d+)-(\d+) |
Example 1: a phone-style number
The pattern \d{3}-\d{4} means three digits, a hyphen, then four digits. Run on the text "call 555-1234 now", it matches 555-1234. The braces say how many times the previous item repeats, so you avoid writing \d\d\d.
Example 2: capture groups and replacing
Parentheses capture parts of a match. The pattern (\d{4})-(\d{2})-(\d{2}) run on "on 2026-11-16" captures 2026, 11 and 16 as groups one, two and three. You can use those groups in a replacement to reorder the date: replacing with $3/$2/$1 turns 2026-11-16 into 16/11/2026.
This is how editors and scripts perform bulk reformatting, such as swapping first and last names or converting date formats, without touching each line by hand.
Example 3: greedy versus lazy
Quantifiers are greedy by default: they match as much as they can. Run <.+> on "<b>x</b>" and it matches the entire string <b>x</b>, because .+ grabs everything up to the last >. Add a question mark to make it lazy: <.+?> stops at the first > and matches just <b>.
This difference causes more regex bugs than anything else. If a pattern is matching too much, try the lazy form or a more specific character class such as <[^>]+>.
Do not use regex to parse full HTML. It handles simple cases but fails on nesting and edge cases. Use an HTML parser for real documents.
Example 4: matching emails, with a warning
A practical pattern is ^[\w.+-]+@[\w-]+\.[\w.]+$. It matches asha@example.com. It is good enough to catch obvious typos in a form, but the real rules for email addresses are far more complicated, and no regular expression validates them perfectly.
For important flows, check the format loosely and then verify the address by sending a message. Overly strict patterns reject valid addresses.
Lookarounds: matching without consuming
Lookbehind and lookahead check what is around a position without including it in the match. The pattern (?<=\$)\d+ finds the digits that follow a dollar sign. Run on "cost $45" it matches only 45, not $45. Lookbehind support depends on the engine, so test it in the environment you will use.
Flags, safety and good habits
- Common flags: g finds all matches, i ignores case, m makes ^ and $ work on each line.
- Escape special characters with a backslash when you mean them literally, for example \. for a dot and \$ for a dollar sign.
- Beware of catastrophic backtracking. Patterns with nested repetition such as (a+)+$ can take exponential time on certain inputs and freeze a program. Avoid them on untrusted text.
- Build patterns step by step and test each stage on sample text, including inputs that should not match.
- Add a comment or name for any complicated pattern. Regexes are write-easy and read-hard.
Frequently asked questions
What does ^ do inside square brackets?
At the start of a character class it negates it, so [^0-9] matches anything that is not a digit. Elsewhere, ^ outside brackets means the start of the text.
Why does my regex match too much?
Quantifiers like .* and .+ are greedy. Make them lazy with a question mark (.*?) or use a more specific character class.
Are regular expressions the same in every language?
The core is shared, but features such as lookbehind, named groups and Unicode handling vary by engine. Test in the language you will use.
What is a capture group?
Text matched inside parentheses is remembered so you can reuse it in a replacement ($1, $2) or read it in code.
Try these tools
Regex Tester
Test JavaScript regular expressions live. See matches highlighted, capture groups and indexes, with flags for global, case-insensitive and more.
Open toolFind and Replace Text
Find and replace text in bulk with plain search or regular expressions. Match case, whole word, capture groups and a live replacement count. Free and private.
Open toolText Diff Checker
Compare two blocks of text side by side and see added and removed lines highlighted. Ignore case and spaces. Free, private, runs in your browser.
Open toolJSON Formatter
Format, validate, beautify and minify JSON instantly. Shows the exact line and column of any error. Free and runs in your browser.
Open toolKeep reading
How to verify web design files
How to verify web design files before you accept, publish or hand them off: check they arrived intact, nothing is missing, the code is valid, images are right and the build matches the design.
Read guideHow JSON Web Tokens work, and how to read one safely
Understand the three parts of a JSON Web Token, why the payload is readable by anyone, what the signature actually protects, and how to inspect a token without leaking it.
Read guideUnix timestamps explained: seconds, milliseconds and the Year 2038 problem
Learn what a Unix timestamp is, how to tell seconds from milliseconds, how to convert one to a human date, why time zones matter, and what the Year 2038 problem is.
Read guide