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?
The replaceAll() method will substitute all instances of the string or regular expression pattern you specify, whereas the replace() method will replace only the first occurrence.
The replace() method searches a string for a value or a regular expression. The replace() method returns a new string with the value(s) replaced. The replace() method does not change the original string.
The String type provides you with the replace() and replaceAll() methods that allow you to replace all occurrences of a substring in a string and return the new version of the string.
In your specific example, the $1 will be the group (^| ) which is "position of the start of string (zero-width), or a single space character". So by replacing the whole expression with that, you're basically removing the variable theClass and potentially a space after it.
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/>');
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/>');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With