最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Replace multiple occurence string using array element - Stack Overflow

programmeradmin0浏览0评论

I have several strings in an associative array:

    var arr = {
        '============================================': '---------',
        '++++++++++++++++++++++++++++++++++++++++++++': '---------',
        '--------------------------------------------': '---------'
    };

I want to replace occurrences of each key with the corresponding value. What I've e up with is:

    for (var i in arr)
    {
        strX = str.replace(i, arr[i]);

        console.log('arr[\''+i+'\'] is ' + arr[i] + ': ' + strX);
    }

This works, but only on first occurence. If I change the regex to /i/g, the code doesn't work.

for (var i in arr)
{
    strX = str.replace(/i/g, arr[i]);

    console.log('arr[\''+i+'\'] is ' + arr[i] + ': ' + strX);
}

Do you guys know how to work around this?

I have several strings in an associative array:

    var arr = {
        '============================================': '---------',
        '++++++++++++++++++++++++++++++++++++++++++++': '---------',
        '--------------------------------------------': '---------'
    };

I want to replace occurrences of each key with the corresponding value. What I've e up with is:

    for (var i in arr)
    {
        strX = str.replace(i, arr[i]);

        console.log('arr[\''+i+'\'] is ' + arr[i] + ': ' + strX);
    }

This works, but only on first occurence. If I change the regex to /i/g, the code doesn't work.

for (var i in arr)
{
    strX = str.replace(/i/g, arr[i]);

    console.log('arr[\''+i+'\'] is ' + arr[i] + ': ' + strX);
}

Do you guys know how to work around this?

Share Improve this question edited Aug 16, 2011 at 1:42 Mike Samuel 121k30 gold badges227 silver badges254 bronze badges asked Aug 16, 2011 at 0:27 ariefbayuariefbayu 22k13 gold badges72 silver badges93 bronze badges 6
  • 2 Don't use for ... in on arrays. As soon as someone touches the array prototype you will be in for a world of hurt. – cdhowie Commented Aug 16, 2011 at 0:30
  • 3 I don`t want to be evil but array is [], and {} is object. – Bakudan Commented Aug 16, 2011 at 0:31
  • @cdhowie: OP isn't actually using an Array. var arr = {...}. – user113716 Commented Aug 16, 2011 at 0:31
  • 1 This is true. The name arr confused me. :) Still, if someone touches the object prototype, the OP will still have issues. – cdhowie Commented Aug 16, 2011 at 0:32
  • Touching the object prototype has a lot of its own issues though, and if I remember correctly, can't be done in IE at all. – LoveAndCoding Commented Aug 16, 2011 at 0:34
 |  Show 1 more ment

3 Answers 3

Reset to default 7

Instead of

strX = str.replace(/i/g, arr[i]);

you want to do something like.

strX = str.replace(new RegExp(i, "g"), arr[i]);

This is because /i/g refers to the letter i, not the value of variable i. HOWEVER one of your base string has plus signs, which is a metacharacter in regexes. These have to be escaped. The quickest hack is as follows:

new RegExp(i.replace(/\+/g, "\\+"), "g"), arr[i]);

Here is a working example: http://jsfiddle/mFj2f/

In general, though, one should check for all the metacharacters, I think.

The i in the regex will be the string i, not the variable i. Try instead new RegExp(i,'g'); and you should get the desired results

   var W,H,A,K;

W='123456'.split(''),

H='test this WHAK  www for'.split(' '),

A='2 is a 1 (1ed 6 3 2, 1 6 3 that), 1ing 6 5.3.4';

K=0;for(K in W)A=A.split(W[K]).join(H[K]);

  document.write(A);

发布评论

评论列表(0)

  1. 暂无评论