I have the following regex in JS - `/[()\/]/g` tha...
# announcements
v
I have the following regex in JS -
/[()\/]/g
that matches
(
,
)
,
/
anywhere in a string. Why exactly the same pattern doesn’t work with Kotlin when I pass it to the
Regex
?
d
Modifies have to be passed separately to the
Regex
constructor. Please show your exact Kotlin code.
v
The code is
Copy code
str.replace(Regex("/[()/]/g"), "")
d
Remove the
/
and
/
at the start and end (they are not part of the regex and just tell JavaScript that this is a regex). Also remove the
g
at the end which is a modifier and needs to be passed via the
option
parameter to the
Regex
constructor.
Actually, the
g
modifier is not a thing in Kotlin's Regex implementation.
v
Yes, I tried that. But it doesn’t match my string
d
You might have to escape the
/
And I have not used that feature before, but regexr.com tells me that that regex should work 😄
v
Just did the same - run it in a scratch file and it work
Turns out the regex checker in IDEA is not fully correct
I trusted them 😞
k
the checker might only check if it's a complete match?
👍🏻 1
n
replace
is a loop around
find
and not
match
. So yes,
str.replace(Regex("[()/]"), "")
should do what you want.