6

Hello see the jsfiddle here : http://jsfiddle.net/moolood/jU9QY/

var toto = 'bien_address_1=&bien_cp_1=&bien_ville_1=';
var tata = toto.replace('&','<br/>');
$('#test').append(tata);

Why Jquery in my exemple only found one '&' and replace it?

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
user367864
  • 283
  • 2
  • 5
  • 11

2 Answers2

12

Because that's how replace works in JavaScript. If the search argument is a string, only the first match is replaced.

To do a global replace, you have to use a regular expression with the "global" (g) flag:

var tata = toto.replace(/&/g,'<br/>');
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
3

The code that you have written will only replace the first instance of the string.

Use Regex along with g will replace all the instances of the string.

toto.replace(/&/g,'<br/>');
Sushanth --
  • 55,259
  • 9
  • 66
  • 105