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.
This is not something that a regex is suited for. No matter how you turn it you need at least three separate replace operations, which means compiling and executing three regexes, and finding a provably non-colliding pattern for the intermediate replacement is a bother. No, in this case you should take the string apart yourself and translate all the characters you want in one go. Much more efficient and reasonable.
(That is, unless Javascript has some Perl-like tr
operator that I was unaware of.)
精彩评论