Regex.replace
func Regex.replace(p: Regex.Pattern, s: string, replacement: string): stringRegex.replace returns s with every match of p replaced by replacement; the matches are the ones Regex.findAll returns. In replacement, $n and ${n} expand to group n, ${name} to a named group, $$ to one $, and a group that took no part to "". A $ that starts none of these, such as one at the end, one before a character that cannot start a name, or a ${ with no name or no }, stays as written. A name after $ is taken as long as it can be, so write ${1}x, not $1x.
Details
$is special inreplacement: write$$for a literal$, or useRegex.replaceWith, which inserts its text as written.- A group number or name that the pattern does not have expands to
""; it is not an error. - A
$that starts no group reference, such as one at the end, stays as written:Regex.replace(/b/, "ab", "$")is"a$". - The matches replaced are the ones
Regex.findAllreturns, so an empty match right after another match is skipped:Regex.replace(/a*/, "baaab", "-")is"-b-b-".
Basic examples
Replace every match of a pattern:
Regex.replace(/\d+/, "a1b22c333", "#") // "a#b#c#"Rearrange the groups of each match with $1, $2:
Regex.replace(/(\w+)@(\w+)/, "ann@home bob@work", "$2:$1") // "home:ann work:bob"When the pattern does not match, the string comes back unchanged:
Regex.replace(/x/, "banana", "o") // "banana"Scope
$0 is the whole match:
Regex.replace(/\d+/, "3 apples, 12 pears", "[$0]") // "[3] apples, [12] pears"Braces end a group number, so text can follow it directly:
Regex.replace(/(\d+)/, "a1b22", `${1}x`) // "a1xb22x"
Regex.replace(/(\d+)/, "a1b22", "\${1}x") // "a1xb22x"A named group expands by its name, as $name or ${name}:
let date = /(?<y>\d{4})-(?<m>\d\d)-(?<d>\d\d)/
Regex.replace(date, "2026-11-12", `${d}.${m}.${y}`) // "12.11.2026"
Regex.replace(/(?<word>\w+)/, "hi there", "<$word>") // "<hi> <there>"$$ gives one $:
Regex.replace(/\d+/, "cost 5", "$$$0") // "cost $5"A group that took no part in a match expands to "":
Regex.replace(/(a)|(b)/, "ab", "[$1|$2]") // "[a|][|b]"A pattern that matches the empty string matches between the characters too:
Regex.replace(/x*/, "abc", "-") // "-a-b-c-"
Regex.replace(/^/m, "a\nb", "> ") // "> a\n> b"
Regex.replace(/(?:)/, "", "x") // "x"
Regex.replace(/./, "", "x") // ""Flags apply as in any match. i ignores case:
Regex.replace(/cat/i, "Cat cat CAT", "dog") // "dog dog dog"\d, \w, and \s are ASCII; \pL matches a letter of any script:
Regex.replace(/\w+/, "naïve", "#") // "#ï#"
Regex.replace(/\pL+/, "naïve ωμέγα", "#") // "# #"The pattern is the first argument, so call it through . on a pattern, or with named arguments:
let vowels = /[aeiou]/
vowels.replace("banana", "_") // "b_n_n_"
Regex.replace(p: vowels, s: "banana", replacement: "_") // "b_n_n_"Applications
Collapse runs of white space into one space, then trim the ends:
Regex.replace(/\s+/, " too many\t spaces \n", " ").trim() // "too many spaces"Turn a title into a URL slug:
func slug(title: string): string {
let dashed = Regex.replace(/[^a-z0-9]+/, title.toLower(), "-")
Regex.replace(/^-|-$/, dashed, "")
}
println(slug("Hello, World! It's 2026!"))hello-world-it-s-2026Mask all but the last four digits of a card number:
Regex.replace(/\d(?:\d|\s)*(\d{4})/, "card 4111 1111 1111 1234", "**** $1") // "card **** 1234"Replace user-supplied text literally: Regex.escape quotes the pattern, and doubling every $ quotes the replacement:
func replaceText(s: string, old: string, new: string): string {
let p = Result.unwrap(Regex.compile(Regex.escape(old)))
Regex.replace(p, s, new.replace("$", "$$"))
}
println(replaceText("price: 1.5 or 1x5", "1.5", "$2"))price: $2 or 1x5Properties and relations
Replacing every match with $0 gives s back:
for s in ["", "a1b22", "no digits"] {
assert(Regex.replace(/\d+/, s, "$0") == s)
}When there is no match, s comes back unchanged:
let s = "banana"
assert(!Regex.test(/x/, s))
assert(Regex.replace(/x/, s, "y") == s)Regex.replace agrees with Regex.replaceWith when the function builds the same text from the match:
let p = /(\w+)@(\w+)/
let s = "ann@home bob@work"
assert(p.replace(s, "$2:$1") == p.replaceWith(s, { "${x.groups[2]}:${x.groups[1]}" }))For a pattern that matches only literal text and a replacement with no $, it agrees with String.replace:
let s = "a.b.c"
assert(Regex.replace(/\./, s, "/") == s.replace(".", "/"))Regex.replaceFirst replaces only the first match:
Regex.replace(/\d/, "a1b2", "#") // "a#b#"
Regex.replaceFirst(/\d/, "a1b2", "#") // "a#b2"Possible issues
A name after $ runs as long as it can, so $1x asks for a group named 1x, which does not exist, and expands to "". _ is part of a name too:
Regex.replace(/(\d+)/, "a1b22", "$1x") // "ab"
Regex.replace(/(\w+)/, "hi there", "$1_") // " "Write ${1}x in a raw string instead, as in Scope.
In an ordinary string literal, ${name} is interpolation, not a group reference:
let s = Regex.replace(/(?<y>\d+)/, "2026", "${y}!") // error: y is not definedWhen a variable of that name exists, the code compiles and inserts its value, which is worse. Use a raw string, `${y}!`.
The pattern comes first, so piping the string into Regex.replace does not compile:
let s = "foo"
println(s | Regex.replace(/o/, "0"))
// error: cannot use string where Regex.Pattern is expected
// error: cannot use Regex.Pattern where string is expectedPass the string as the second argument, or call replace on the pattern: /o/.replace(s, "0").
Regex.replace returns a new string and leaves s as it was:
let s = "a b"
Regex.replace(/ /, s, "_") // "a_b"
s // "a b"\d matches only ASCII digits, and i folds case one code point at a time, so ß does not match SS:
Regex.replace(/\d/, "1 ٣", "#") // "# ٣"
Regex.replace(/straße/i, "STRASSE", "street") // "STRASSE"See also
Regex.replaceFirst · Regex.replaceWith · Regex.findAll · Regex.split · Regex.escape · Regex.compile · Regex.Match · String.replace · Regex literals · Raw strings
