Rueta
Rueta

Reputation: 3577

how to replace multi string with a array node in js

i here a Work and i don't know how to do it. i have a string here:

<div class="demotext">
   <p>this is demo string i demo want to demo use</p>
</div>

i create the array variable for demo:

var demoarray = new array('a','b','c');

now i want replace 'demo' in string by array node, follow 'demo' one change to 'a' , 'demo' two change to 'b'....

Upvotes: 0

Views: 687

Answers (2)

kennytm
kennytm

Reputation: 523684

.

function sequential_replace(str, replacementRx, arr) {
   var i = 0;
   return str.replace(replacementRx, function() {
      return arr[i++];
   });
}

return sequential_replace("this is demo string i demo want to demo use",
                          /demo/g, ['a', 'b', 'c']);

Upvotes: 0

vsync
vsync

Reputation: 130620

var string = 'this is demo string i demo want to demo use';
var demoarray = ['a','b','c'];

for(i=0; i < demoarray.length; i++){
    string = string.replace('demo',demoarray[i] );
}

alert(string) // "this is a string i b want to c use"

Upvotes: 4

Related Questions