I want to find a /
symbol on given text and replace with *
symbol.
http://localhost:7070/home/test/mobile:device.testdevice.id1_123.id2_456.id3_ab-c.CONSTANT_formula:map/TreeMap.json
Here the CONSTANT is fixed and it will not get changed but after CONSTANT there can be any character. I want find the CONSTANT in the url and look for /
symbol and replace it with *
symbol.
Something like this – http://localhost:7070/home/test/mobile:device.testdevice.id1_123.id2_456.id3_ab-c.CONSTANT_formula:map*TreeMap.json
I am trying with below regular expression but it matches everything after CONSTANT as I use one or more identifier
^CONSTANT(.+)
Is there any way find the / symbol and replace with * symbol?
2
Answers
Using
mod_rewrite
rule you can do this to replace/
with*
after keywordCONSTANT
:Note that it will perform an external redirect after replacement in URL.
We are using 2 capture groups in
RewriteCond
:^(/.*CONSTANT[^/]*)
matches everything uptoCONSTANT
followed by 0 or more of any char that is not/
/
(.*)
: Finally we match everything else in 2nd capture groupBy using JavaScript, just use String.prototype.replace.
Let the regex seperates the string into two parts from
CONSTANT
. Replace all/
to*
in the latter one.