0

Say I had the following code:

var str = "abcde";
str.replace("a", "ab")
str.replace("b", "c")

The value of str go from abcde to abbcde to acccde. How would I go about making it so that the two operations happen simultaneously, so that the value of str instead goes from abcde to abccde?

EDIT: I'm talking about any situation similar to this, not just this specific one.

Invariance
  • 777
  • 1
  • 5
  • 14
  • why do you replace `a` with `ab` if you know that you'll replace that `b` with a `c`? – Thomas Feb 09 '18 at 02:05
  • I'm just using it as an example. I'm talking about any general case. – Invariance Feb 09 '18 at 02:20
  • Hello, since you unaccepted my answer and went for one that replaces by character, maybe you should edit the question to say "Replace multiple characters in string with different strings"? You did not say anything about the search strings all being one character so many of the answers went for the regex approach. – T Tse Feb 13 '18 at 00:37

4 Answers4

1

When replace contains these kind of patterns, first do which is indepndent and then other like below.

var str = "abcde";
str.replace("b", "c");
str.replace("a", "ab");

Or else replace both of them using some map:

var str = "abcde";
var mapObj = {
   a:"ab",
   b:"c"
};
str = str.replace(/a|b/gi, function(matched){
  return mapObj[matched];
});
console.log(str);

Output:

abccde
Tilak Putta
  • 634
  • 4
  • 16
1

You need to match both a and b with a regular expression, then use a function to distinguish whether it was an a or an b. See documentation for the replace function.

var str = 'abcde'
var newStr = str.replace(/[ab]/g, function (match) {
  switch (match) {
    case 'a':
      return 'ab'
    case 'b':
      return 'c'
    default:
      return '???' // or throw error
  }
})
console.log(newStr) // -> abccde

If you have a list of strings that you want to replace, you might want to look at how to escape strings to be used in regular expressions, then use the constructor for RegExp to construct the regular expression to be used for the replace function.

var replacements = {'a': 'ab', 'b': 'c'}
var keys = Object.keys(replacements)
// escapeRegExp function from https://stackoverflow.com/a/6969486/8557739
var escapedKeys = keys.map(escapeRegExp)
var re = RegExp(escapedKeys.join('|'), 'g') // re = /a|b/g

// then continue on like before
var str = 'abcde'
var newStr = str.replace(re, function (match) {
  for (var key in replacements) {
    if (match === key) {
      return replacements[key]
    }
  }
  return '???' // or throw error
})
console.log(newStr) // -> abccde
T Tse
  • 680
  • 5
  • 18
0

var str = "abcde"; str.replace(/a|b/gbbc);

You can use this regex as well, as it will replace 'a' with 'b' and 'b' with 'bc'. If you want to apply this regex globally for your String then you can use '/g' as mentioned above else just a / after b. To chain replacement, use '|' operator as I did between a|b.

Adya
  • 888
  • 6
  • 16
0

I have found a way to do it using arrays.

var str = "abcde";
var str2 = str.split("");
for(var i = 0; i < str2.length; i++){
    if(str2[i] === "a"){
        str2[i] = "ab"
    } else if(str2[i] ===  "b"){
        str2[i] = "c";
    }
}
Invariance
  • 777
  • 1
  • 5
  • 14