How to remove ↵ character from JS string

后端 未结 4 639
北海茫月
北海茫月 2020-12-15 12:42

I have a string like

↵my name is Pankaj↵

I want to remove the character from the string.

\"↵select * from e         


        
相关标签:
4条回答
  • 2020-12-15 12:49

    Instead of using the literal character, you can use the codepoint.

    Instead of "↵" you can use "\u21b5".

    To find the code, use '<character>'.charCodeAt(0).toString(16), and then use like \u<number>.

    Now, you can use it like this:

    string.split("\u21b5").join(''); //split+join
    
    string.replace(/\u21b5/g,'');  //RegExp with unicode point
    
    0 讨论(0)
  • 2020-12-15 12:57

    works fine.

    If it works fine, then why bother? Your solution is totally acceptable.

    but I want to know is there is any other way to remove these kind of unnecessary characters.

    You could also use

    "↵select * from eici limit 10".split("↵").join("") 
    
    0 讨论(0)
  • 2020-12-15 13:13

    You can also try regex as well,

    "↵select * from eici limit 10".replace(/↵/g, "")
    
    0 讨论(0)
  • 2020-12-15 13:15

    With something like

    s.replace(/[^a-zA-Z0-9_ ]/g, "")
    

    you can for example keep only alphabetic (a-zA-Z), numeric (0-9) underscores and spaces.

    Some characters require to be specified with a backslash in front of them (for example ], -, / and the backslash itself).

    You are however going to run into problems when your start deploying to an international audience, where "strange characters" are indeed the norm.

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