Javascript 数字和结尾空格的正则表达式

Javascript 数字和结尾空格的正则表达式,javascript,regex,Javascript,Regex,我试图验证一些表单,我想要的是一个允许数字和空格的正则表达式。表达式将不允许 1000 space space 00 但它会允许这样的事情发生 space space space 10000 或 我已经试过了,但这不是我想要的: var filter = /^[0-9 ]+$/; if(price.val() == "") alert('vide'); else if(filter.test(price.val())) alert('number'); else

我试图验证一些表单,我想要的是一个允许数字和空格的正则表达式。表达式将不允许

1000 space space 00
但它会允许这样的事情发生

space space space 10000

我已经试过了,但这不是我想要的:

var filter = /^[0-9 ]+$/;
if(price.val() == "") 
    alert('vide');
else if(filter.test(price.val()))
    alert('number');
else 
    alert('no number');
希望你能帮助我。

你可以试试

var filter = /^\s*[0-9]+\s*$/;
是空白字符。如果您只想专门检查空格,可以将其替换为

您可以尝试

var filter = /^\s*[0-9]+\s*$/;

是空白字符。如果只想专门检查空格,可以将其替换为

可以使用如下正则表达式:

^\s*\d+\s*$

工具中的正则表达式解释:

^                        the beginning of the string
\s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                         more times)
\d+                      digits (0-9) (1 or more times)
\s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                         more times)
$                        before an optional \n, and the end of the
                         string

您可以使用这样的正则表达式:

^\s*\d+\s*$

工具中的正则表达式解释:

^                        the beginning of the string
\s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                         more times)
\d+                      digits (0-9) (1 or more times)
\s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                         more times)
$                        before an optional \n, and the end of the
                         string

希望此模式有助于
^*[0-9]+*$

希望此模式有助于
^*[0-9]+*$

我想我被编辑错了:(我想我被编辑错了:(非常确定“空格”在原始帖子中,空格是指空格字符,而不是单词space。不过误解是有道理的。在原始帖子中,
1000空格00
语句没有作为代码块封装。编辑将它们放在代码块中。我删除了否决票。你的正则表达式很好,因为你认为用户想要什么…现在你已经这一部分也搞定了!另外,谢谢你的链接。我知道有很多regex网站,但这一个看起来不错。@MadPhysicator regex101是一个很好的网站。你也可以尝试debuggex.com,这也是一个很好的工具,hwnd创造了非常确定的“空间”在原始帖子中,空格是指空格字符,而不是单词space。不过误解是有道理的。在原始帖子中,
1000空格00
语句没有作为代码块封装。编辑将它们放在代码块中。我删除了否决票。你的正则表达式很好,因为你认为用户想要什么…现在你已经这一部分也搞定了!另外,谢谢你的链接。我知道有很多regex网站,但这一个看起来不错。@MadPhysicator regex101是一个很好的网站。你也可以尝试debuggex.com,这也是hwnd创建的一个很好的工具