jQuery 替换 unicode 空格字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16416610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 17:04:42  来源:igfitidea点击:

Replace unicode space characters

javascriptjqueryregex

提问by user2025749

I need to replace the unicode characters defined on here

我需要替换此处定义的 unicode 字符

I have got this so far but it seems to remove all space including standard spacebar ones:

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

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

Result - Hellothisisatestofthesite

结果 - Hellothisisatestofthesite

I only want to remove the unicode character which is U+2003 between 'test' and 'of' in the string.

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

回答by Guffa

Remove the regular space that you have first in the pattern:

删除您在模式中首先拥有的常规空间:

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

回答by Kuf

try this:

尝试这个:

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

same as you did, but with out ' ' (regular space)

和你一样,但没有 ' '(常规空格)

回答by elclanrs

I think you can simplify, try this:

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

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

Demo: http://jsbin.com/acexun/2/edit

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

回答by user1251840

I guess you should use the escape sequence for spaces (15.10.2.12 of the norm) which is \s, and that you want to replace multiple spaces by a single one :

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

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