Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i replace text inside variable with jquery or javascript [duplicate]

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

like image 305
Mirage Avatar asked Dec 19 '25 00:12

Mirage


1 Answers

You might use a regular expression

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

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

  • regex are heavily optimised on all browsers, especially if using the /\[\[myvar\]\]/ notation
  • if your pattern is dynamic you may use new RegExp(somepattern, 'g') (but for better performances don't use this if you have a fixed pattern)
like image 177
Denys Séguret Avatar answered Dec 20 '25 14:12

Denys Séguret



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!