Monday, April 2, 2018

.replace in JavaScript only replaces the first match in a string and .replaceAll only exists in jQuery so how may I have a .replaceAll emulation in other JavaScript frameworks?

You use a hodgepodge of .split and .join like so to achieve the same effect.

var xml = "<foo><bar>Bar</bar><baz>Baz</baz><qux>Qux</qux></foo>";
var changeOne = xml.split("<").join("&lt;");
var changeTwo = changeOne.split(">").join("&gt;");
alert(changeTwo);

No comments:

Post a Comment