开发者

Special character checking in JavaScript

开发者 https://www.devze.com 2022-12-11 00:47 出处:网络
I have a string: var string = \"asdasASFASDŞGFSD123435489()/%&&&&&&&&&&&&&&&123435\";

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开发者_JAVA百科 want the output:

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

How can I accomplish this in JavaScript?


well, mine is cooler ;)

 var string = "asdasASFASDŞGFSD123435489()/%&&&&&&&&&&&&&&&123435";
 alert(string.replace(/&+/g, "$& "))


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 ");
0

精彩评论

暂无评论...
验证码 换一张
取 消