Replace unicode space characters

前端 未结 4 597
轮回少年
轮回少年 2020-12-11 05:07

I need to replace the unicode characters defined on here

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

va         


        
相关标签:
4条回答
  • 2020-12-11 05:33

    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,'');
    
    0 讨论(0)
  • 2020-12-11 05:34

    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 :

    str= str.replace(/\s+/g,' ') ;
    
    0 讨论(0)
  • 2020-12-11 05:38

    I think you can simplify, try this:

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

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

    0 讨论(0)
  • 2020-12-11 05:41

    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)

    0 讨论(0)
提交回复
热议问题