Swapping a character for another in Regex/Javascript

旧街凉风 提交于 2021-01-01 04:05:02

问题


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?


回答1:


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




回答2:


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.




回答3:


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.)



来源:https://stackoverflow.com/questions/3902042/swapping-a-character-for-another-in-regex-javascript

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!