javascript 如何去除?JS 字符串中的字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29097921/
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
How to remove ? character from JS string
提问by ipankaj
I have a string like
我有一个像
?my name is Pankaj?
I want to remove the ?
character from the string.
我想?
从字符串中删除字符。
"?select * from eici limit 10".replace("?", "")
Works fine. But I want to know if there is any other way to remove these kind of unnecessary characters.
工作正常。但我想知道是否有其他方法可以删除这些不必要的字符。
采纳答案by Ismael Miguel
Instead of using the literal character, you can use the codepoint.
您可以使用代码点,而不是使用文字字符。
Instead of "?"
you can use "\u21b5"
.
而不是"?"
您可以使用"\u21b5"
.
To find the code, use '<character>'.charCodeAt(0).toString(16)
, and then use like \u<number>
.
要查找代码,请使用'<character>'.charCodeAt(0).toString(16)
,然后使用 like \u<number>
。
Now, you can use it like this:
现在,您可以像这样使用它:
string.split("\u21b5").join(''); //split+join
string.replace(/\u21b5/g,''); //RegExp with unicode point
回答by Bergi
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("")
回答by Merianos Nikos
You can also try regex
as well,
您也可以尝试regex
为好,
"?select * from eici limit 10".replace(/?/g, "")
回答by 6502
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.
例如,您可以只保留字母 ( a-zA-Z
)、数字 ( 0-9
) 下划线和空格。
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.
然而,当你开始向国际观众部署时,你会遇到问题,“奇怪的角色”确实是常态。