```val a = Regex("foo") a.replace("foobar", "\\w")...
# announcements
w
Copy code
val a = Regex("foo")
a.replace("foobar", "\\w")
res3: kotlin.String = wbar
i would expect this to return
\\wbar
- am I missing something here? Isn't
\\
an escape for the
\
?
n
Kotlin strings replace
\\
with a single
\
and `\ is special in replacements to quote e.g. the
$
which normally indicates a back reference. So if you welly want
\\bwbar
then you have to use either
Regex("foo").replace("foobar", "\\\\\\\\w")
or
Regex("foo").replace("foobar", """\\\\w""")
(which sidesteps the Kotlin String part)