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
  • 959,406
  • 173
  • 1,780
  • 1,769
user367864
  • 293
  • 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
  • 959,406
  • 173
  • 1,780
  • 1,769
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 --
  • 54,565
  • 8
  • 62
  • 98