简体   繁体   中英

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. 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 .

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 / .

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

 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.

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.]+) .

If you need a fancier domain matcher (as per your question), just substitute that part of the regex.

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