Skip to content

Instantly share code, notes, and snippets.

@rain-1
Created August 3, 2018 00:10
Show Gist options
  • Save rain-1/a8d1bb04ad01ebe6a4ac68fa33f7961c to your computer and use it in GitHub Desktop.
Save rain-1/a8d1bb04ad01ebe6a4ac68fa33f7961c to your computer and use it in GitHub Desktop.
multirex
var text = "foobar";
text = text.replace(/o/g, "a");
text = text.replace(/a/g, "x");
document.write(text);
var text = "foobar";
var re1 = /o/g;
var re2 = /a/g;
text = text.replace(re1, "a");
text = text.replace(re2, "x");
document.write(text);
var text = "foobar";
var re1 = /o/;
var re2 = /a/;
var re = new RegExp(re1.source + "|" + re2.source, "g");
function repl(m) {
if(m.match(re1)) {
return m.replace(re1, "a")
}
if(m.match(re2)) {
return m.replace(re2, "x")
}
}
text = text.replace(re, repl);
document.write(text);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment