简体   繁体   English

如何在数字正则表达式中保留空格?

[英]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]*$

Online fiddle.re demo . 在线fiddle.re演示

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

相关问题 正则表达式,只允许字符或空格 - Regular expression which allow only characters or space 用于检查字符串的正则表达式,必须包含空格并且是字母数字 - Regular expression for checking a string, which must contain a space and be alpha numeric 正则表达式,允许字母数字与空格并禁止某些特殊字符(&lt;&gt;&;) - Regular expression to allow alphanumeric with space and disallow certain special characters(<>&;) javascript正则表达式允许名称带有一个空格和特殊字母 - javascript regular expression allow name with one space and special Alphabets 正则表达式允许逗号和空格分隔的数字列表 - Regular expression to allow comma and space delimited number list Javascript 只允许字母表、空格和 - 的正则表达式 - Javascript regular expression that allow only alphabet, space and - only 正则表达式在开始或结束时没有空格,但允许中间有空格但也只允许一个字符输入 - Regular expression for no space at start or end, but allow space in middle but also allow only one char inputs 如何编写正则表达式以允许在日期中使用破折号 - how to write the regular expression to allow dashes in the date 如何使用空格获取正则表达式中的字符串 - How to get string in regular expression with space 如何在Javascript中的正则表达式中添加空格 - How to add white space in regular expression in Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM