3

Possible Duplicate:
Why does javascript replace only first instance when using replace?

I have this variable

var newRow = "<td><div> [[myvar]]</div> <div> [[myvar]]</div> </td> "

When i do this

newRow  = newRow.replace('[[myvar]]', '10');

Only first occurance gets replaced and not the second

1

2 Answers 2

9

You might use a regular expression

newRow  = newRow.replace(/\[\[myvar\]\]/g, '10');

There is no other simple solution for multiple replacements. Note that :

Sign up to request clarification or add additional context in comments.

3 Comments

i don't understood , if i have exact match , why do i need to have regex. i mean function replace is same why regex works but not simple string
In Javascript the replace function is actually based on regex. By default the replace hits only the first occurrance. The g flag set the global match option.
thanks buddy. i don't know when i see regex , i heart starts pumping faster like i saw tiger in front of me
0

Use this:

newRow = newRow.replace(new RegExp('[[myvar]]', 'g'), '10');

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.