[英]How to allow space in numeric regular expression?
I want to validate 'int' field with javascript regular expression. 我想使用javascript正则表达式验证“ int”字段。
I am using this RE string 我正在使用此RE字符串
var numbers =/^[0-9]+$/;
This expresion does not allow spaces in the text box. 此表达式不允许在文本框中使用空格。
How do I create a regular expression which allows for spaces in the text box? 如何创建一个允许在文本框中留有空格的正则表达式?
添加可选空格:
var numbers =/^\s*[0-9]+\s*$/;
Add a space to the character set: 在字符集中添加一个空格:
/^[0-9 ]+$/
If you want to guarantee that at least one digit exists, you could use a lookahead: 如果要保证至少有一位数字存在,可以使用前瞻:
/^(?=\s*\d)[\d\s]+$/
此正则表达式允许数字和空格。
/[^\d\s]/
It depends where you want to allow spaces and whether you want input of spaces only (no digits) to be allowed. 这取决于您要在哪里允许空格以及是否只允许输入空格(无数字)。 If you want to require at least one digit and any distribution of spaces, you can do this: 如果要要求至少一位数字和空格的任何分布,可以执行以下操作:
/^(\s*\d+)+\s*$/
This will allow input such as " 202 555 1212 " 这将允许输入诸如“ 202 555 1212”
This will permit to use spaces, tabs and digits, but require to use at least one digit: 这将允许使用空格,制表符和数字,但要求至少使用一位数字:
^\s*\d[\d\s]*$
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.