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.

2

5 Answers 5

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

Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

Please don't use a while loop like that :-/
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] ;
});

1 Comment

Nice and clean, short answer! +1
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)
}

2 Comments

Look at @Nina's answer. Its similar but covers more cases
@Rajesh I added a note about Nina's implementation and added another way to do this.
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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.