Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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 come 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
    
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 Aug 16 '11 at 0:30
2  
I don`t want to be evil but array is [], and {} is object. –  Bakudan Aug 16 '11 at 0:31
    
@cdhowie: OP isn't actually using an Array. var arr = {...}. –  user113716 Aug 16 '11 at 0:31
    
This is true. The name arr confused me. :) Still, if someone touches the object prototype, the OP will still have issues. –  cdhowie Aug 16 '11 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. –  Ktash Aug 16 '11 at 0:34
show 1 more comment

2 Answers

up vote 4 down vote accepted

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.net/mFj2f/

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

share|improve this answer
2  
There'll actually be an issue with the '++++++++' one since + needs to be escaped. –  user113716 Aug 16 '11 at 0:35
1  
Sadly there is no builtin way to escape special regex chars :( –  user166390 Aug 16 '11 at 0:39
    
Darn, no \Q, \E or Pattern.quote, r'...' or %r{...} in JS.... I'm still looking, though. I'll find something for the OP unless someone beats me to it. :-) –  Ray Toal Aug 16 '11 at 0:45
3  
I think you may need to do an .indexOf() test for +, then do a .replace(/\+/g,'\\+'). –  user113716 Aug 16 '11 at 0:48
1  
Thanks for the commentary, pst and patrick. I was a little confused why the fiddle was not working at first! +1 to patrick and pst. –  Ray Toal Aug 16 '11 at 0:52
show 2 more comments

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.