I have a ruby regexp searching for urls and I'm trying to replace them with a certain type of urls.
So this regexp detects any url but excludes the type I replace with.
However, when I try this code :
s.sub(/(?!\S*\/el\/\d*)(?=(?i)\b((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|(([^\s()<>]+|(([^\s()<>]+)))*))+(?:(([^\s()<>]+|(([^\s()<>]+)))*))))/, 'http://localhost:8080/el/133710')
if s is s = "blablabla www.google.com bla http://localhost blabla"
It should replace www.google.com
with my string http://localhost:8080/el/133710
but I get this instead :
"blablabla http://localhost:8080/el/133710www.google.com bla http://localhost blabla"
That is because your intended capture www.google.com
matches inside a lookahead (?=)
, so it is not actually captured. www.google.com
only works as a lookahead context. What is captured is only an empty string at the position of match. sub
is replacing the captured string (which is an empty string) with http://localhost:8080/el/133710
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.