Find and Replace with Regex: A Collection of Practical Patterns
13 JavaScript regex find-and-replace patterns for text cleanup: reformat dates, mask phone numbers, drop parenthesized text and use $1 group references.
Find and replace supports both a plain mode and a regex mode. Plain mode searches for exactly the characters you type, and regex mode searches by pattern. This guide covers the rules of regex mode and frequently used patterns.
Regex rules in this tool
- The syntax is the browser's JavaScript regular expressions.
- The
g(replace all),m(multiline) andu(Unicode) flags are added automatically. Turning on 'Ignore case' addsi. - Because of the
mflag,^and$match the start and end of each line, not of the whole text. - Because of the
uflag, unnecessary backslashes are errors. For example, you cannot write\:; write:instead. Unicode properties such as\p{L}can be used. - In the replacement,
$1,$2are capture groups,$&is the whole match and$$is a single$character.\nbecomes a line break and\ta tab. - If the pattern has an error, the step leaves its input unchanged and shows an error.
\salso matches line breaks. To match spaces only without crossing lines, use[ \t].
Practical patterns
Each entry below is in the form 'search → replacement'. If the replacement is '(empty)', leave the field blank.
1. Change a date format
(\d{4})-(\d{2})-(\d{2}) → $1.$2.$3
2026-09-23 → 2026.09.23
To change the order, rearrange the group numbers, as in $3/$2/$1.
2. Mask the middle of a mobile phone number
(01[016789])-?(\d{3,4})-?(\d{4}) → $1-****-$3
010-1234-5678 → 010-****-5678 01098765432 → 010-****-5432
This pattern is written for Korean mobile numbers, which start with 010, 011 and so on. Adjust the first group for other formats.
3. Mask the start of an email address
\b([A-Za-z0-9._%+-]{2})[A-Za-z0-9._%+-]*@ → $1***@
hong.gildong@example.com → ho***@example.com
4. Remove parentheses and their contents
\s*\([^)]*\) → (empty)
Seoul(HQ) Busan (branch) → Seoul Busan
Nested parentheses such as ((a)) may not be removed in one pass.
5. Remove trailing spaces
[ \t]+$ → (empty)
6. Reduce three or more line breaks to one blank line
\n{3,} → \n\n
7. Remove thousands separators from numbers
(\d),(?=\d{3}\b) → $1
$1,234,567 → $1234567
8. Remove the query and hash from URLs
(https?:\/\/[^\s?#]+)[?#]\S* → $1
https://example.com/page?utm_source=a#top → https://example.com/page
9. Remove timestamps at the start of chat logs
^\[?\d{1,2}:\d{2}(?::\d{2})?\]?[ \t]* → (empty)
[12:30] Hello there → Hello there
10. Swap 'Last, First' order
^([^,\n]+),[ \t]*(.+)$ → $2 $1
Kim, Minsu → Minsu Kim
11. Collapse a repeated word
\b(\w+)[ \t]+\1\b → $1 (with 'Ignore case' on, it also catches The the)
12. Add a space between Korean and Latin letters
(\p{Script=Hangul})([A-Za-z]) → $1 $2
한글abc → 한글 abc
Change the script name, for example to \p{Script=Han}, to apply the same idea to other writing systems.
13. Wrap numbers in brackets
\d+(?:\.\d+)? → [$&]
Common mistakes
- Using a dot (.) as is: in a regex,
.means any single character. To find a period or the dot in a domain, write\..example.comalso matchesexampleXcom. - The greedy .\*:
<.*>grabs everything on a line from the first<to the last>. Applied to<b>x</b> <i>y</i>, it removes the text as well. To match one tag at a time, use<[^>]*>or<.*?>. - $ in the replacement: in regex mode, to put a
$in the replacement, as in a price, you must write$$. In plain mode,$is inserted literally. - Finding special characters literally: to find
( ) [ ] + ? *, put a backslash in front. If there are many symbols, it is simpler to turn off regex and search in plain mode.
Working across lines
Putting \n in the search text lets you find the line break itself. For example, ,\n → , joins lines ending in a comma with the following line. If your goal is to delete whole lines, Filter lines is simpler than a regex. For splitting text into lines sentence by sentence, the 'Sentence end' mode of Add line breaks (split) is safer.
Order matters
You can put several find and replace steps in one pipeline, and they are applied in order from the top. The output of an earlier step becomes the input of the next, so patterns often only match after spaces have been cleaned up. Putting Trim leading and trailing spaces and Collapse repeated spaces and tabs first lets you write simpler patterns.
Checklist
- Use
^and$for line-based patterns. - Use
[ \t]instead of\sto avoid crossing lines. - Check the result; if it is wrong, switch the step off and fix the pattern.
Last updated: 2026-09-23