I'm looking to do something like:
var a = "This is an A B pattern: ABABA";
a.replace("A", "B");
a.replace("B", "A");
and have it return:
=> "This is an B A pattern: BABAB"
instead of:
=> "This is an A A pattern: AAAAA"
My first thought is to do something like:
a.replace("A", "improbablePattern");
a.replace("B", "A");
a.replace("improbablePattern", "B");
How should I really be doing it?
You could pass a function to your .replace() call with the regex that matches both, like this:
var a = "This is an A B pattern: ABABA";
a = a.replace(/(A|B)/g, function(a, m) { return m == "A" ? "B" : "A"; });
alert(a); //alerts "This is an B A pattern: BABAB"
You can try it out here
You need to do all replacements at once. You can use something like this to do that:
String.prototype.translate = function(translation) {
var names = [];
for (var search in translation) {
if (translation.hasOwnProperty(search)) {
names.push(search.replace(/([.\\+*?[^\]$(){}=!<>|:\-])/g, "\\$1"));
}
}
var re = new RegExp("(?:" + names.join("|") + ")", "g");
return this.replace(re, function(val) { return translation[val]; });
}
var text = "This is an A B pattern: ABABA",
translation = {
"A": "B",
"B": "A"
};
text = text.translate(translation);
Here translation is a mapping from old onto new values. Use this with the here defined translate method on strings to do the translation. The property names of the passed translation object are used to build a regular expression of the form (?:name1|name2|…|nameN). That regular expression is then used for the replacement while an additional function is used to return the new value that corresponds to the matched value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With