简体   繁体   English

JS:正则表达式-用全文替换部分域

[英]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. 我将整个html页面存储在一个变量中,我想更改某些符合条件的URL。

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". 我们只在寻找某些顶级域名,比如说“ .XYZ”,如果找到它,我们希望结尾是“ .XYZ.ABC”-仅添加“ .ABC”。

For example <a href="http://example.xyz/">link</a> would be changed to <a href="http://example.xyz.abc/">link</a> 例如, <a href="http://example.xyz/">link</a>将更改为<a href="http://example.xyz.abc/">link</a>

But if domain already has ".abc" ending we should leave it alone. 但是,如果域已经以“ .abc”结尾,我们应该将其保留。 I would like to change text of all links, so src, href and also js values like var link="123.xyz/1.bmp"; 我想更改所有链接的文本,因此src,href以及js值(例如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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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