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'....
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"
.
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']);
精彩评论