简体   繁体   中英

JS: Regex - replace parts of domains in full text

I'm confronted with a problem. I have a whole html page stored in one variable and I would like to change certain URLs that meets criteria.

We are looking only for certain top level domain, let's say ".XYZ" and if we find it we would like the ending to be ".XYZ.ABC" - just adding ".ABC".

For example <a href="http://example.xyz/">link</a> would be changed to <a href="http://example.xyz.abc/">link</a>

But if domain already has ".abc" ending we should leave it alone. I would like to change text of all links, so src, href and also js values like var link="123.xyz/1.bmp";

Other examples

www.123.xyz -> www.123.xyz.abc
abc.xyz/1.bmp -> abc.xyz.abc/1.bmp
eee.xyz.abc -> eee.xyz.abc

这样的事情应该起作用:

text.replace(/\.xyz(?!\.abc)/g, ".xyz.abc")

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM