Skip to content
Advertisement

How could I replace repeated complex strings in javascript? [closed]

For example, the input would be:

@[facts.::ip](facts.::ip) = "127.0.0.1" AND @[facts.::os](facts.::os) = "ubuntu"

I would like to transform that into:

facts.::ip = "127.0.0.1" AND facts.::os = "ubuntu"

Thanks in advance!

I have tried to check for regex, but I do not have experience with it

Advertisement

Answer

Below regular expression method achieves required string transformation.

let from = '@[facts.::ip](facts.::ip) = "127.0.0.1" AND @[facts.::os](facts.::os) = "ubuntu"';
let to = from.replace(/(@[([^])]+)](2))/g, "$2");
console.log(to);
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement