Javascript 正则表达式删除字母,除数字以外的符号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6649327/
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
Regex to remove letters, symbols except numbers
提问by MacMac
How can you remove letters, symbols such as ∞§??aoo?≥≤÷
but leaving plain numbers 0-9, I want to be able to not allow letters or certain symbols in an input field but to leave numbers only.
你怎么能删除字母、符号,比如∞§??aoo?≥≤÷
但留下纯数字 0-9,我希望能够在输入字段中不允许字母或某些符号,而只留下数字。
Demo.
演示。
If you put any symbols like ? # ¢ ∞ § ? ? a
or else, it still does not remove it from the input field. How do you remove symbols too? The \w
modifier does not work either.
如果您放置任何符号,例如? # ¢ ∞ § ? ? a
或 else,它仍然不会将其从输入字段中删除。你如何删除符号?该\w
修改也不起作用。
回答by alex
回答by Klemen Tu?ar
Use /[^0-9.,]+/
if you want floats.
/[^0-9.,]+/
如果你想要浮动,请使用。
回答by bezmax
Simple:
简单的:
var removedText = self.val().replace(/[^0-9]+/, '');
^ - means NOT
^ - 表示不是
回答by Darin Dimitrov
回答by Aziz Shaikh
If you want to keep only numbers then use /[^0-9]+/
instead of /[^a-zA-Z]+/
如果您只想保留数字,请使用/[^0-9]+/
代替/[^a-zA-Z]+/