简体   繁体   English

替换正则表达式匹配的字符串的字符

[英]Replace characters of a string matched by regex

I am in a situation to find the domain name of all valid URLs among a HTML page, replace these domain names with another domain name, but within the domain name, I need to do a 2nd replacement.我的情况是在 HTML 页面中找到所有有效 URL 的域名,将这些域名替换为另一个域名,但在域名内,我需要进行第二次替换。 For example, say the url https://www.example.com/path/to/somewhere is among the HTML page, I need to eventually transfer it into something like www-example-com.another.domain/path/to/somewhere .例如,假设 url https://www.example.com/path/to/somewhere位于 HTML 页面中,我最终需要将其转移到类似www-example-com.another.domain/path/to/somewhere

I can do the first match and replace with the following code:我可以进行第一次匹配并替换为以下代码:

    const regex = new RegExp('(https?:\/\/([^:\/\n\"\'?]+))', 'g');

    txt = txt.replace(regex, "$1.another.domain");

but I have no idea how to do the second match and replace to replace the .但我不知道如何进行第二场比赛并替换以替换. into - .进入- I wonder if there is any efficient way to finish this task.我想知道是否有任何有效的方法来完成这项任务。 I tried to do something like the following but it does not work:我试图做类似以下的事情,但它不起作用:

    const regex = new RegExp('(https?:\/\/([^:\/\n\"\'?]+))', 'g');

    txt = txt.replace(regex, "$1".replace(/'.'/g, '-') + ".another.domain");

Ok - I think I know what you're looking for.好的 - 我想我知道你在找什么。 I'll explain what it's doing.我会解释它在做什么。

You 2 capture groups: the one before and the one after the first / .您有 2 个捕获组:第一个/之前之后的一个。

You're taking the first capture group, and converting the .您正在获取第一个捕获组,并将. to --

You're adding via string .another.domain and then you're appending the 2nd capture group on it afterward您通过字符串.another.domain添加,然后在其上附加第二个捕获组

 const address1 = 'https://www.example.com/path/to/somewhere'; const newDomain = "another.domain" const pattern = /(https?:\/\/[^:\/\n\"\'?]+)(\/.*)/; const matches = pattern.exec(address1); const converted = matches[1].replace(/\./g, "-") + `.${newDomain}${matches[2]}`; console.log(converted);

You can use the function version of String.prototype.replace() to have some more control over the specific replacements.您可以使用String.prototype.replace()的 function 版本对特定替换进行更多控制。

For example...例如...

 const txt = 'URL is https://www.example.com/path/to/somewhere' const newTxt = txt.replace(/(https?:\/\/)([\w.]+)/g, (_, scheme, domain) => `${scheme}${domain.replace(/\./g, '-')}.another.domain`) console.log(newTxt)

Here, scheme is the first capture group (https?:\/\/) and domain is the second ([\w.]+) .在这里, scheme是第一个捕获组(https?:\/\/)domain是第二个([\w.]+)

If you need a fancier domain matcher (as per your question), just substitute that part of the regex.如果您需要更高级的匹配器(根据您的问题),只需替换正则表达式的那部分即可。

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

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