Reputation: 9400
I have an URL like that:
http://www.url.me/en/cats/dogs/potatoes/tomatoes/
I need to replace the first two REST parameters to get a result URL like that:
http://www.url.me/FIRST/cats/dogs/potatoes/tomatoes/
I tried this regex \/([^/]+)\/
but it's not working as expected in CF:
<cfset ret.REDIRECT = reReplace(currentUrl, "\/([^/]+)\/", "FIRST", "all") />
What do you suggest, both for the regex and the cf code?
Thank you.
Upvotes: 1
Views: 99
Reputation: 112170
Firstly, you do not need to escape /
in regex. (Sometimes you'll see it escaped, such as in JavaScript regex literals, but that is the JS side being escaped, not the regex.)
However, even with that change it wont do what you want - you'll be replacing every other /-qualified segment instead of just the first one after the host part.
To do what you want, use something like this:
reReplace(CurrentUrl, "^(https?://[^/]+/)[^/]+/", "\1FIRST/")
^
anchors the replace to the start of the input.(..)
part captures the protocol and hostname so they can be re-inserted with \1
in the replacement string.[^/]+/
is what captures the first part of the request uri and replaces it with the FIRST/
in the replacement string.(You can omit the trailing /
if it's not required, or use (?=/)
to assert that it is there without needing to put it in the replace side.)
Upvotes: 2