Regular expressions (regex) are patterns for finding and replacing text. They look cryptic at first, but a handful of building blocks covers most everyday tasks: validating an email field, extracting dates from logs, cleaning up spreadsheets or renaming files.
Page Contents
Character classes
| Pattern | Matches |
|---|---|
. | Any character except a line break |
\d | A digit 0–9 |
\w | A letter, digit or underscore |
\s | A space, tab or line break |
\D, \W, \S | The opposite of the above |
[abc] | a, b or c |
[a-z] | Any lowercase letter |
[^0-9] | Anything except a digit |
Quantifiers
| Pattern | Means |
|---|---|
* | 0 or more |
+ | 1 or more |
? | 0 or 1 (optional) |
{3} | Exactly 3 |
{2,5} | 2 to 5 |
{2,} | 2 or more |
Quantifiers are greedy – they match as much as possible. Add ? to make them lazy: <.+?> matches one HTML tag instead of everything between the first < and the last >.
Anchors and boundaries
| Pattern | Means |
|---|---|
^ | Start of the line (with the m flag) or text |
$ | End of the line or text |
\b | Word boundary – \bcat\b matches “cat” but not “category” |
Groups
(abc)– a capture group; use$1in replacements.(?:abc)– a group without capturing.(?<year>\d{4})– a named group.a|b– a or b (alternation).(?=…)and(?!…)– lookahead: followed by / not followed by.
Flags (JavaScript)
- g – find all matches, not just the first
- i – ignore case
- m – ^ and $ match at each line
- s – . also matches line breaks
- u – full Unicode support
Ready-to-use patterns
| Purpose | Pattern |
|---|---|
| Simple email check | ^[^\s@]+@[^\s@]+\.[^\s@]+$ |
| Date YYYY-MM-DD | ^\d{4}-\d{2}-\d{2}$ |
| Hex colour | ^#?([0-9a-fA-F]{3}){1,2}$ |
| Hashtag | #\w+ |
| Empty lines | ^\s*$ (with m flag) |
| Extra spaces | {2,} → replace with one space |
Email tip: a perfect email regex is extremely long. A simple check plus a confirmation email is more reliable.
Practical examples
- Swap date format: find
(\d{2})/(\d{2})/(\d{4}), replace with$3-$2-$1to turn 31/12/2026 into 2026-12-31. - Remove HTML tags: find
<[^>]+>and replace with nothing (fine for simple text, not for parsing full HTML). - Find duplicated words:
\b(\w+)\s+\1\bfinds “the the”.
Avoid slow patterns
Nested quantifiers like (a+)+ can take extremely long on some inputs (“catastrophic backtracking”). Keep patterns specific and test them with long text in the Regex Tester.
FAQ
What does \d mean in regex?
Any digit from 0 to 9.
What is the difference between * and +?
* matches zero or more times; + matches one or more times.
How do I match a literal dot?
Escape it with a backslash: \. matches only a dot.
Are regex patterns the same in every language?
The basics are shared, but features and syntax differ slightly between JavaScript, Python, Java and others.
Is my text sent anywhere?
No. The tester uses your browser’s JavaScript regex engine.
Need a website or app for your business? Talk to Techpass Master – we design and build websites and apps for clients worldwide.
