Replace text using regular expressions
How regex find-and-replace works, when it beats plain find-and-replace, and how Unicode, engine syntax and copying affect the result.
A regular expression replacement finds every stretch of text that matches a pattern and swaps in new text, often reusing pieces of what it matched. It is precise and fast for structured edits, and it is also easy to get subtly wrong. This review covers when regex replacement fits the job, what it actually changes, and how the output holds up once it is copied, shared or read by other software.
Is regex replacement the right tool for your job?
Regex replacement is a good fit when the text has a repeatable structure, such as dates, IDs, log lines, names in a fixed order or runs of whitespace, and you can preview the matches before applying them. Its main benefit is that one pattern covers many variations, and capture groups let you rearrange matched parts instead of retyping them. The decisive tradeoff is that the pattern applies everywhere it matches, including places you did not anticipate, and the syntax for both patterns and replacements differs between engines.
- Plain literal find-and-replace is the better choice when you are changing one fixed word or phrase.
- It is also better when the search text contains characters such as . * ? + ( ) [ ] { } ^ $ | or a backslash, which have special meaning in a pattern and would need escaping.
- It is better for one-off edits short enough to check by eye.
- It is better when other people who maintain the text or script cannot read or verify the pattern.
Whichever you choose, keep a copy of the original text and review the list of matches, or a before-and-after comparison, before running a replace-all.
What a regex replacement actually changes
A regex replacement changes the stored characters. This separates it from CSS styling, mirrored or flipped display effects, and bidirectional rendering, which change how text looks without changing the underlying characters. After a replacement, the new text is what gets copied, saved, searched and indexed. It is not a font change or a visual effect.
The word "reverse" is ambiguous in this context. In regex usage it usually means reordering captured parts, for example turning "Doe, Jane" into "Jane Doe". It rarely means reversing a string character by character. A single pattern-and-replacement pass cannot reverse text of arbitrary length. That task needs a scripting step, such as a replacement callback or a separate string function.
Anatomy of a replacement
- Pattern: describes what to match. Parentheses create capture groups.
- Flags: control behavior. Common ones are global (replace all matches, not just the first), case-insensitive, multiline (^ and $ match at line breaks) and dot-all (. also matches newlines). Flag names and availability vary by engine.
- Replacement: the text to insert. It can refer back to capture groups or to the whole match.
Pattern: (\d{4})-(\d{2})-(\d{2})
Replacement: $2/$3/$1 (JavaScript-style references, global flag on)
Input: Invoice due 2026-10-31, reminder 2026-11-07
Output: Invoice due 10/31/2026, reminder 11/07/2026
Check the example against its edges. The pattern requires exactly four, two and two digits, so "2026-1-5" is left unchanged. That may be intended, or it may be a silent miss. The pattern also matches inside longer digit runs: "12026-10-310" becomes "110/31/20260". Adding word boundaries, as in \b(\d{4})-(\d{2})-(\d{2})\b, prevents that partial match.
Replacement syntax differs by engine
| Engine or tool | Reference to group 1 | Reference to whole match |
| JavaScript (String replace) | $1 | $& |
| Python (re.sub) | \1 or \g<1> | \g<0> |
| Java (Matcher replaceAll) | $1 | $0 |
| .NET (Regex.Replace) | $1 | $0 or $& |
| sed | \1 | & |
A replacement string written for one engine can produce literal "$1" or "\1" text in another. Escaping rules for a literal dollar sign or ampersand also differ. Online testers usually emulate one or more specific flavors, so confirm which flavor a tester uses before moving a pattern into code. No specific tester was evaluated for this review.
Greedy versus lazy matching
Input: <b>bold</b>
Pattern: <.*> (greedy) matches: <b>bold</b> (the whole string)
Pattern: <.*?> (lazy) matches: <b> and </b>
Quantifiers like * and + are greedy by default and take as much text as possible. Many unexpected replace-all results come from a greedy match swallowing more than intended.
Accented letters, combining marks and emoji
Many accented letters can be stored in two ways. One is a single precomposed character. The other is a base letter followed by a combining mark. Both render the same, but a pattern written in one form will not match text in the other. A pattern aimed at a plain base letter can also match the start of a decomposed character and leave the accent attached to the replacement. Normalizing both the text and the pattern to the same form (NFC is common) avoids this.
Input (decomposed): cafe\u0301 displays as: café
Pattern: \u00E9 (precomposed é) result: no match
Pattern: e Replacement: E result: cafE\u0301 displays as: cafÉ
Emoji raise a related problem. In JavaScript without the u flag, the dot matches one UTF-16 code unit, so it can split an emoji in half and leave broken characters. With the u flag, the dot matches a whole code point. Emoji built from several code points, such as flags, skin-tone variants and joined sequences like family emoji, can still be split. Character classes also differ by engine. In JavaScript, \w covers only ASCII letters, digits and underscore. In Python 3, \w is Unicode-aware by default for string patterns. A pattern that works on plain English can therefore skip accented names in one engine and not another.
Will the result stay readable and usable after copying or sharing?
Because the change is to the characters themselves, ordinary regex output copies and pastes intact. It does not depend on a stylesheet or font to look right, and it is as searchable and as readable by assistive technology as any other plain text. Problems come from what a replacement inserts or damages, not from the method itself.
- Broken characters: split emoji or orphaned combining marks may appear as boxes, question marks or misplaced accents in other apps.
- Invisible characters: a literal space in a pattern does not match a non-breaking space, although \s does in JavaScript and Python 3. Zero-width characters left behind can break search, sorting and form validation without being visible.
- Line endings: text from different systems may use \r\n or \n. A pattern written for one convention can leave stray carriage returns in the output.
- Stylized lookalike characters: if a replacement inserts Unicode "font" characters, such as mathematical bold or italic letters, the result looks styled but is made of different characters. Search may not find the original words, and screen readers may announce them oddly. Behavior varies by screen reader and was not tested here. Keep a plain-text version wherever the text must be found, read aloud or edited.
- Reading order: reordering capture groups changes the logical order of the text, which is what search and assistive technology use, so verify the new order reads correctly and not only that it looks right.
A reliable check is to compare before and after with a diff tool, confirm the number of replacements matches what you expected, and run the pattern on deliberately awkward samples. Good samples include accented names, emoji, empty lines and text pasted from other systems.
A note on online regex testers and sensitive text
A tester's appearance or marketing does not show whether text is processed locally or sent to a server, or whether it is stored. Check the tool's own documentation or privacy statement. For confidential data, a local editor or script is the safer default.
What this review could not verify
No specific regex tool, editor or tester was examined, and no measurements or test results were available. Engine behavior described here reflects general documented behavior of the named languages. Details can change between versions and options, so confirm flag names, replacement syntax and Unicode handling in the documentation for the exact engine you use.