简体   繁体   English

正则表达式删除字母,除数字以外的符号

[英]Regex to remove letters, symbols except numbers

How can you remove letters, symbols such as ∞§¶•ªºº«≥≤÷ 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.你怎么能删除字母,符号,如∞§¶•ªºº«≥≤÷但留下普通数字0-9,我希望能够在输入字段中不允许字母或某些符号,而只留下数字。

Demo .演示

If you put any symbols like ¡ € # ¢ ∞ § ¶ • ª 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. \w修饰符也不起作用。

You can use \D which means non digits .您可以使用\D ,这意味着非数字

var removedText = self.val().replace(/\D+/g, '');

jsFiddle . js小提琴

You could also use the HTML5 number input .您也可以使用HTML5数字输入

<input type="number" name="digit" />

jsFiddle . js小提琴

Use /[^0-9.,]+/ if you want floats.如果需要浮点数,请使用/[^0-9.,]+/

Simple:简单的:

var removedText = self.val().replace(/[^0-9]+/, '');

^ - means NOT ^ - 表示不

Try the following regex:尝试以下正则表达式:

var removedText = self.val().replace(/[^0-9]/, '');

This will match every character that is not ( ^ ) in the interval 0-9.这将匹配区间 0-9 中不是( ^ ) 的每个字符。

Demo .演示

If you want to keep only numbers then use /[^0-9]+/ instead of /[^a-zA-Z]+/如果您只想保留数字,请使用/[^0-9]+/而不是/[^a-zA-Z]+/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 正则表达式:删除除字母和分隔符之外的所有内容 - Regex: remove everything except the letters and separator 正则表达式匹配字母和数字,除非连续三个字母(任意) - Regex match letters and numbers, except if there are three consecutive (any) letters 正则表达式仅允许 1-31 个数字并禁用字母和符号 - Regex to allow only 1-31 numbers and disable letters & symbols 正则表达式删除除数字以外的所有特殊字符? - Regex remove all special characters except numbers? 我如何构建一个正则表达式来排除除字母和数字之外的所有内容? - How can I build a regex to exclude all, except letters and numbers? RegExp 限制所有符号,除了数字和后面的几个字母(如果它们持续存在) - RegExp restrict all symbols except numbers and several letters after them if they persist 如何替换JavaScript字符串中除字母和数字之外的所有内容(空格/符号)? - How do I replace everything(whitespaces/symbols) except letters and numbers in a JavaScript String? 正则表达式字母/数字组合 - Regex For Letters/Numbers combination 正则表达式功能4个字母和4个数字? - regex function 4 letters and 4 numbers? Javascript:如何按数字然后字母然后符号对数组进行排序? - Javascript: How to sort array in by numbers then letters then symbols?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM