Useful replacements
| Task | Find | Replace with | Regex |
|---|---|---|---|
| Put each comma-separated item on its own line | , |
\n |
Off |
| Swap “Last, First” to “First Last” | ^(\w+), (\w+)$ |
$2 $1 |
On |
| Wrap every line in quotes and add a comma | ^(.*)$ |
"$1", |
On |
| Remove trailing commas before } or ] (JSON) | ,(\s*[}\]]) |
$1 |
On |
| Remove HTML tags | <[^>]+> |
(empty) | On |
| Turn tabs into commas (TSV to CSV, simple cases) | \t |
, |
Off |
Replacing text in code
text.replaceAll('http://', 'https://'); // plain text, every match
text.replace(/(\w+)@(\w+)/g, '$2 at $1'); // regex with groups
// Escape user input before putting it in a regex
const escaped = find.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
text.replace(new RegExp(escaped, 'gi'), replacement);
import re
text.replace("http://", "https://")
re.sub(r"(\w+)@(\w+)", r"\2 at \1", text) # Python uses \1, not $1
re.sub(re.escape(find), replacement, text, flags=re.IGNORECASE)
sed 's/http:/https:/g' file.txt # print the result
sed -i '' 's/http:/https:/g' file.txt # in place (macOS; GNU sed: -i without '')
sed -E 's/([a-z]+)@([a-z]+)/\2 at \1/g' file.txt
Building a complicated pattern? The regex tester highlights every match and shows the groups. Want to check what changed? Paste the before and after into the diff checker.
Frequently asked questions
How do I use capture groups in the replacement?
Turn on “Regular expression”, put parentheses around the parts you want to keep, and refer to them as $1, $2 and so on in the replacement. Named groups (?<year>\d{4}) are referred to as $<year>, $& inserts the whole match, and $$ inserts a literal dollar sign. These are JavaScript’s rules; Python and many editors use \1 instead of $1.
How do I replace text with a line break?
Type \n in the “Replace with” field; it’s read as a line break as long as the “\n and \t” option is on. You can search for \n in the same way, to join lines with a comma for example. Turn the option off if you need to find the literal two characters backslash and n.
What does “Whole words only” match?
The search text only when it isn’t part of a longer word, so id matches “id” and “id.” but not “idx” or “_id”. Unlike \b in a JavaScript regex, it also works with accented and non-Latin letters: café won’t match inside caféine.
Do ^ and $ match at each line?
Yes. In regex mode the multiline flag is on, so ^ and $ match at the start and end of every line, which is what you want for line-by-line edits. The dot doesn’t match line breaks; use [\s\S] to match across lines.
What if my regex hangs?
Patterns with nested repetition like (a+)+ can take exponential time on some inputs. Regexes run in a background worker that is stopped after 1.5 seconds, so the page never freezes, and you get a message telling you what to look for. Test the pattern in the regex tester to see where it backtracks.