1

I want my Javascript to do this with a string...

Find the first instance of "aaa" and replace it with "xxx". Find the second instance of "aaa" and replace it with "yyy". Find the third instance of "aaa" and replace it with "zzz".

...and continue doing that until it no longer finds "aaa" in that string.

So for example, this string...

HELLO aaa WORLD aaa HERE aaa IS aaa SOME aaa RANDOM aaa TEXT aaa FOR aaa TESTING aaa

...will become this string...

HELLO xxx WORLD yyy HERE zzz IS xxx SOME yyy RANDOM zzz TEXT xxx FOR yyy TESTING zzz

I have Googled like hell about Javascript replacement, arrays, loops, etc., but everything I have tried has been unsuccessful. Any ideas from any Javascript coders out there?

By the way, I am not a jQuery user. So any code that relies on jQuery will not be useful for me.

Invernagep
  • 11
  • 1

5 Answers5

6

You could use a replacement function with String#replace and take an array as parameter as well as a starting index for replacement.

function replaceWith(array, i) {
    i = i || 0;
    return function () {
        i %= array.length;
        return array[i++];
    }
}

var string = 'HELLO aaa WORLD aaa HERE aaa IS aaa SOME aaa RANDOM aaa TEXT aaa FOR aaa TESTING aaa';

string = string.replace(/aaa/g, replaceWith(['xxx', 'yyy', 'zzz']));

console.log(string);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
4

you can do it in the following way

let str = 'HELLO aaa WORLD aaa HERE aaa IS aaa SOME aaa RANDOM aaa TEXT aaa FOR aaa TESTING aaa';

let replacements = ['xxx', 'yyy', 'zzz'], idx = 0;
while(str.match(/aaa/)){
    str = str.replace(/aaa/, replacements[idx]);
    idx = (idx+1)%3;
}

console.log(str);
marvel308
  • 10,288
  • 1
  • 21
  • 32
3
var text = "HELLO aaa WORLD aaa HERE aaa IS aaa SOME aaa RANDOM aaa TEXT aaa FOR aaa TESTING aaa";
var set = ["xxx", "yyy", "zzz"] ;
var i = 0;

text = text.replace(/aaa/g, function() {
    return set[i++ % set.length] ;
});
Cerbrus
  • 70,800
  • 18
  • 132
  • 147
nithinTa
  • 1,632
  • 2
  • 16
  • 32
1

You can use String.prototype.replace() by passing it a function as the second parameter to replace the string:

var replace = ["xxx", "yyy", "zzz"]
var index = 0

str.replace(/aaa/g, function(x) {
    index %= replace.length
    return replace[index++]
})

Please note that this code may not work on all browsers because the documentation doesn't say anything about the order of the function calls.

If you want to support all browsers, then you could use a loop like this which is probably faster than most of the other answers which use loops:

var replace = ["xxx", "yyy", "zzz"]
var index = 0

var result = ""
var copy = str

while (true) {
    var pos = copy.indexOf("aaa")
    if (pos == -1) break

    index %= replace.length
    result += copy.substring(0, pos)
    result += replace[index++]

    // The length of "aaa" is 3.
    copy = copy.substring(pos + 3)
}
0

try this

function replaceWord(i) {
  const replacedItems = ['xxx', 'yyy', 'zzz']
  var i = i || 0;
  return () => {
    i %= replacedItems.length
    return replacedItems[i++]
  }
}

const input = 'HELLO aaa WORLD aaa HERE aaa IS aaa SOME aaa RANDOM aaa TEXT aaa FOR aaa TESTING aaa'

const result = input.replace(/aaa/g, replaceWord())

console.log(result)
Alongkorn
  • 3,968
  • 1
  • 24
  • 41