替换unicode空格字符

时间:2013-05-07 10:19:44

标签: javascript jquery regex

我需要替换here

上定义的unicode字符

到目前为止我已经得到了这个,但它似乎删除了所有空间,包括标准空格键:

var str = "Hello this is a test  of the site"; 
str= str.replace(/[ \u00A0\u1680​\u180e\u2000-\u2009\u200a​\u200b​\u202f\u205f​\u3000]/g,'')

结果 - Hellothisisatestofthesite

我只想删除字符串中'test'和'of'之间的U + 2003的unicode字符。

4 个答案:

答案 0 :(得分:8)

删除模式中第一个的常规空间:

 str = str.replace(/[\u00A0\u1680​\u180e\u2000-\u2009\u200a​\u200b​\u202f\u205f​\u3000]/g,'');

答案 1 :(得分:4)

试试这个:

var str = "Hello this is a test  of the site";
str= str.replace(/[\u00A0\u1680​\u180e\u2000-\u2009\u200a​\u200b​\u202f\u205f​\u3000]/g,'')

与你一样,但没有''(常规空间)

答案 2 :(得分:1)

我认为你可以简化,试试这个:

str = str.replace(/(?! )\s/g,'');

演示:http://jsbin.com/acexun/2/edit

答案 3 :(得分:-1)

我猜你应该使用空格的转义序列(规范的15.10.2.12),它是\ s,并且你想用一个空格替换多个空格:

str= str.replace(/\s+/g,' ') ;