var x = new Array('1','2','3');
var y = new Array('a','b','c');
var iLen = x.length;
var s = 'abcdefgabcdefg';
for (var i=0;i<iLen;i++) {
var re = new RegExp(x[i],'g');
s = s.replace(y[i], re);
}
alert(s);
I want the result to be 123defg123defg. Instead, I get /1/g/2/g/3/gdefgabcdefg.
You are doing it wrong, because you want to replace occurrences of array y
to occurrences of x
globally, you should say like
for (var i=0;i<iLen;i++) {
var re = new RegExp(y[i],'g'); //this is regexp for global y[i]
s = s.replace(re, x[i]); //replace all occurrences of y[i] with x[i]
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다