Home>

I think the cause is abString = abString.replace (), but I don't know the specific improvement.
The code below is a simple representation of the problem I am experiencing. I'm trying to reverse a and b.

var abString = "aabb abab";// The goal is to replace this string to bbaa baba
var searchList = ["a", "b"];// Search for characters in this list
searchList.forEach (function (val) {
    var regexp = new RegExp (val, "g");
    console.log (val, regexp, abString)
    if (abString.match (regexp)) {
        var hitList = abString.match (regexp);
        for (var a = 0;a<hitList.length;a ++) {
            if (val == "a") {
                abString = abString.replace (val, "b");
            } else {
                abString = abString.replace (val, "a");
            }
        }
    }
});
console.log (abString);// aaaa aaaa is displayed.
  • Answer # 1

    Take a

    replacecallback and make it a table lookup (MDN). If it is replaced at once, it is irrelevant even if there is overlap between before and after replacement.

    var replaceTable = {a: 'b', b: 'a'}
    'abbcbba'.replace (/ [ab]/g, (str) =>replaceTable [str]);
    // The result is 'baacaab'

  • Answer # 2

    val =="a"is entered directly, so which character is fixed to which character?
    If you take out one character at a time, you can do it without replacing. * As a way of doing it

    var abString = "aabb abab";
    var searchList = ["a", "b"];
    var replaceAbString = "";
    for (i = 0;i<abString.length;i ++) {
        if (searchList.indexOf (abString [i])>= 0) {
            if (abString [i] == "a") {
                replaceAbString + = "b";
            } else {
                replaceAbString + = "a";
            }
        } else {
            replaceAbString + = abString [i];
        }
    }
    console.log (replaceAbString);// bbaa baba

    It may be more versatile if this character is converted to the conversion list format.

    var searchList = {"a": "b", "b": "a"};
    // The process after this changes a little, but no replacement is necessary

  • Answer # 3

    Unverified.

    'aaabbb'.replace (/ [ab]/g, funcction (char) {
      return this [char];
    } .bind ({a: 'b', b: 'a'}));

    Re: StackOverflowpeta

  • Answer # 4

    aabb abab
    ↓ Replace a with b
    bbbb bbbb
    ↓ Replace b with a
    aaaa aaaa

    That's right.
    It's just an example, so I can't say anything because I don't know the character string that I want to replace, but I think it's easier to understand how to replace it with another symbol that has nothing to do with it.

    aabb abab
    ↓ Replace a with% and remember% = b
    %% bb% b% b
    ↓ Replace b with # and remember # = a
    %% ##% #% #
    ↓ Replace% with b
    bb ## b # b #
    ↓ Replace # with a
    bbaa baba

    I think there are other smarter ways.

  • Answer # 5

    Plan 1:
    Specify a function for replacement.

    Plan 2:
    Replace with another character. In other words, the replacement is performed three times.

    var out1 = abString.replace (new RegExp (searchList.join ("|"), "g"),
        function (m) {
            return m == searchList [0]?
                searchList [1]:
                searchList [0];
        }
    )
    var out2 = abString.replace (new RegExp (searchList [0], "g"), "\ 0")
                .replace (new RegExp (searchList [1], "g"), searchList [0])
                .replace (/ \ 0/g, searchList [1]);

Related articles