I have a string like
↵my name is Pankaj↵
I want to remove the ↵
character from the string.
\"↵select * from e
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
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("")
You can also try regex
as well,
"↵select * from eici limit 10".replace(/↵/g, "")
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.