【问题标题】:Replace unicode space characters替换 unicode 空格字符
【发布时间】:2013-05-01 06:18:39
【问题描述】:

我需要替换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,'')

结果 - Hellothisistestofthesite

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

【问题讨论】:

    标签: javascript jquery regex


    【解决方案1】:

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

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

    【讨论】:

    • @Bojangles:空格字符就像黑洞;只有通过它们如何影响其他可见的事物才能看到它们。 :)
    • 说得很好。我在 OP 中没有注意到它,因为它没有格式化为代码
    • 谢谢我一开始完全错过了那个空间。
    【解决方案2】:

    试试这个:

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

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

    【讨论】:

      【解决方案3】:

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

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

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

      【讨论】:

      • 这个正则表达式是什么意思?
      【解决方案4】:

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

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

      【讨论】:

        猜你喜欢
        • 2017-10-15
        • 2018-04-19
        • 2011-04-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-08
        相关资源
        最近更新 更多