Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace only occuring in first element

Can someone help on why this is only replacing the '[m]' in the first LI? The rest of them stay as '[m]'

  aData[2] = "<li>1[m]</li><li>2[m]</li><li>3[m]</li>"

  $.html( aData[2].replace('[m]','[media]') )
like image 716
Control Freak Avatar asked Dec 17 '22 04:12

Control Freak


1 Answers

Use a regular expression and make it global:

$.html( aData[2].replace(/\[m\]/g,'[media]') )
like image 165
Alex Avatar answered Dec 31 '22 17:12

Alex