Regular expressions have a reputation for being unreadable, and long ones often are. But you don’t need to master the whole language to get value from it. A handful of short patterns covers most everyday jobs: finding numbers, cleaning up whitespace, pulling dates out of text, and doing smarter find-and-replace in your editor.
Below are ten patterns worth knowing, what each piece means, and where each one falls short. All examples use JavaScript syntax, which most online testers and code editors understand.
Try it: Regex TesterTest JavaScript regular expressions with live highlighting, capture groups, flags, a replace preview and a library of common patterns.
Open tool — freeA two-minute crash course
You only need a few building blocks to read the patterns below:
\dmatches a digit,\wa letter, digit or underscore,\sany whitespace (space, tab, line break).+means one or more,*zero or more,?optional,{4}exactly four,{2,}two or more.[abc]matches any one of those characters;[^abc]matches anything except them.^and$anchor to the start and end of the text (or of each line with themflag).( )creates a capture group you can reuse in a replacement as$1,$2and so on.\bis a word boundary, the edge between a word character and anything else.
Flags change behavior: g finds all matches, i ignores case, m makes ^ and $ work per line.
10 patterns you’ll actually use
1. Any number: \d+
Matches runs of digits like 42 or 2026. It won’t match decimals or negatives as a single number. For those, try -?\d+(\.\d+)?, which allows an optional minus sign and an optional decimal part. It still ignores thousands separators like 1,200.
2. A whole word: \bcat\b
Finds cat but not category or concatenate. Add the i flag to catch Cat too. Word boundaries are based on \w, so they can behave unexpectedly with accented letters or hyphenated words.
3. Leading and trailing whitespace: ^\s+|\s+$
With the g flag, replace with nothing to trim both ends of a string. Add the m flag to trim every line of a multi-line block. Most languages also have a built-in trim(), which is clearer when you’re writing code.
4. Repeated spaces: {2,}
That’s a space followed by {2,}. Replace with a single space to collapse double spaces left after editing. Use [ \t]{2,} to include tabs. Avoid \s{2,} here unless you also want to merge line breaks.
5. Empty lines: ^\s*$
With the g and m flags, this matches lines that are empty or contain only whitespace. Replacing them can leave stray line breaks behind, so for this particular job a dedicated tool is often less fiddly (more on that below).
6. ISO dates: \d{4}-\d{2}-\d{2}
Matches dates like 2026-03-14. Be aware it also accepts 2026-99-99. Regex checks shape, not meaning. Validate actual dates with a date library.
7. Reformat dates with groups: (\d{4})-(\d{2})-(\d{2})
Replace with $2/$3/$1 to turn 2026-03-14 into 03/14/2026. Capture groups are the single most useful regex skill for find-and-replace, because they let you rearrange text instead of just deleting it.
8. Email-ish: [^\s@]+@[^\s@]+\.[^\s@]+
Matches something, an @, something, a dot, something. That’s deliberately loose. Real email syntax is complicated enough that no reasonable regex gets every case right, and a pattern that’s too strict will reject real customers. Use a loose check to catch obvious typos, then confirm the address by sending an email to it.
9. URLs in text: https?:\/\/\S+
Finds links starting with http:// or https:// up to the next whitespace. It will often grab trailing punctuation, such as the period at the end of a sentence, so review the matches or trim them afterward.
10. Hex colors: #[0-9a-fA-F]{6}\b
Matches six-digit colors like #1A2B3C in CSS. Extend it to #([0-9a-fA-F]{3}){1,2}\b to include short forms like #fff. Eight-digit colors with alpha need another tweak.
How to test a pattern before you trust it
Never run a new regex on real data without testing it on examples first, especially in a replace-all. Our free regex tester uses JavaScript’s regex engine, highlights matches live, shows capture groups and previews replacements. It also includes a library of common patterns to start from.
- Paste a realistic sample of your text, including the messy cases: blank lines, odd punctuation, edge values.
- Type your pattern and set the flags you need, usually
g, plusiormas required. - Check what’s highlighted, and just as important, what isn’t.
- Look at the capture groups if you plan to use
$1and friends. - Try the replacement in the preview and compare the result to what you expected.
- Copy the pattern into your code or editor only once it handles the awkward examples.
If your code is in Python, PHP, Java or another language, test there too. Most of the basics above behave the same everywhere, but flags, escaping and advanced features differ between regex flavors.
When not to use regex
- Parsing HTML, JSON or CSV. Use a proper parser. Regex breaks on nesting and quoting rules.
- Strict validation of complex formats like email addresses, URLs or dates. Use a dedicated library, or check loosely and verify another way.
- Routine text cleanup. Removing duplicate lines, trimming spaces, sorting a list or stripping HTML tags is faster with a purpose-built tool. Our duplicate line remover and text cleaner does those jobs in one click, with no pattern to debug.
- Anything a teammate will need to maintain if a few lines of plain code would be clearer. Readable beats clever.
Habits that save time
- Start small and build up. Get
\d{4}working before writing the whole date pattern. - Escape special characters you mean literally:
.?+*()[and others need a backslash. - Remember that
.*is greedy and grabs as much as it can..*?grabs as little as possible. - Leave a comment in your code explaining what the pattern is for, with an example of a match.
The takeaway
Learn the building blocks, keep these ten patterns handy, and test every pattern on messy real-world samples before running it on real data. Treat regex as a tool for finding and reshaping text, not for validating complex formats, and reach for a parser or a simple cleanup tool when the job calls for one.
Frequently asked questions
Is there a perfect regex for email addresses?
Not in practice. The full email specification is complicated, so most patterns are approximations that accept some invalid addresses or reject rare valid ones. For real validation, check the basic shape and then send a confirmation email.
What does the g flag do in regex?
In JavaScript, the g (global) flag finds every match instead of stopping at the first one. It matters most for replacements, where without it only the first match gets replaced.
Why does my regex work in one tool but not another?
Regex engines differ. JavaScript, Python, PCRE and others support slightly different features and syntax, such as lookbehind or named groups, so test in the same flavor your code uses.



