tags:

views:

55

answers:

2

I have a string:

var string = "asdasASFASDŞGFSD123435489()/%&&&&&&&&&&&&&&&123435";

I want this string to have a space after any group of ampersands. So in this example, I want the output:

"asdasASFASDŞGFSD123435489()/%&&&&&&&&&&&&&&& 123435";

How can I accomplish this in JavaScript?

+1  A: 

I think this is the job of regular expressions. For example you could do something like this (not tested, just to get the idea):

string.replace("(&+)", "$1 ");
Konamiman
+1  A: 

well, mine is cooler ;)

 var string = "asdasASFASDŞGFSD123435489()/%&&&&&&&&&&&&&&&123435";
 alert(string.replace(/&+/g, "$& "))
stereofrog
blgnklc
in the replacement string, does mere `$` stand for the whole matched string?
Amarghosh
stereofrog
Thanks. Now that I know that, yours is cooler. +1
Amarghosh