Regex and rewrite cheat sheet
A regex rule tests one pattern against the whole link, including https://. The pattern can match anywhere in the link. Case does not matter. A pattern that will not compile never matches.
Regex pieces
Section titled “Regex pieces”| Piece | Meaning | Example |
|---|---|---|
\. | A literal dot. | zoom\.us |
. | Any one character. | v. matches v1 |
[abc] | One character from the set. | [xy]\.com |
[a-z0-9] | One character from the range. | [a-z0-9]+ |
[^/] | One character that is not in the set. | [^/]+ matches up to the next slash |
* | The piece before it, zero or more times. | .* |
+ | The piece before it, one or more times. | [0-9]+ |
? | The piece before it, zero or one time. | (www\.)? |
{n} and {n,m} | The piece before it, n times, or n to m times. | [0-9]{4} |
^ and $ | The start and the end of the link. | ^https:// |
a|b | Either a or b. | twitter\.com|x\.com |
( ) | A group. Also captures the text it matched for a rewrite. | (twitter\.com|x\.com) |
Rewrite templates
Section titled “Rewrite templates”A rewrite uses the Replace with field. Its hint text is $1, $2....
| Template | Inserts |
|---|---|
$1 through $9 | The text captured by the first through ninth group. |
$0 | The whole matched text. |
Only the matched part of the link changes. Everything else stays. If the result is the same as the original link, no rewrite happens. The result must still be a valid link.
Worked examples
Section titled “Worked examples”These are the patterns from three presets under URL Rewrites in Browse Presets.
| Preset | Pattern | Replacement | Before | After |
|---|---|---|---|---|
| Twitter/X → xcancel | ://(www\.)?(twitter\.com|x\.com) | ://xcancel.com | https://twitter.com/user/status/123 | https://xcancel.com/user/status/123 |
| Reddit → Old Reddit | ://(www\.)?reddit\.com | ://old.reddit.com | https://reddit.com/r/programming | https://old.reddit.com/r/programming |
| YouTube → Invidious | ://(www\.)?(youtube\.com|youtu\.be) | ://yewtu.be | https://youtube.com/watch?v=abc | https://yewtu.be/watch?v=abc |