简体   繁体   English

正则表达式仅数字和空格

[英]Regular Expression only digits and blank spaces

I have a text input and I require that it only accepts digits (0-9) and blank spaces (" "). 我有一个文本输入,我要求它只接受数字(0-9)和空格(“”)。 The current regexp to validate this input is: 验证此输入的当前正则表达式是:

/((\d{1,})(\s{1,})?){1,}/

Which stands for: one or more groups base on a first group of one or more digits and an optional second group of one or more blank spaces 它代表:一个或多个组基于一个或多个数字的第一组和一个或多个空格的可选第二组

That will only let me introduce values as: 999999 (only digits) or " " (only blank spaces) or 91 08 8510 903 (mix of digits and spaces). 这只会让我将值引入: 999999 (仅数字)或" " (仅空格)或91 08 8510 903 (数字和空格的混合)。 But actually, I also can insert aaa or other characters. 但实际上,我也可以插入aaa或其他字符。

Dissection 解剖

Your regular expression doesn't accept only letters : 您的正则表达式不接受字母:

/((\d{1,})(\s{1,})?){1,}/.test('aaa') // false

Actually, any character is accepted if the input contains at least one digit : 实际上,如果输入包含至少一个数字,则接受任何字符:

/((\d{1,})(\s{1,})?){1,}/.test('a1a') // true

That being said, let's skim the fat from your pattern : 话虽这么说,让我们从你的模式中撇开脂肪:

"{1,}" equals "+"   -> ((\d+)(\s+)?)+
"(.+)?" equals ".*" -> ((\d+)\s*)+
useless brackets    -> (\d+\s*)+

This result can be translated to : "one or more digits ( \\d+ ) followed by zero or more blank spaces ( \\s* ), one or more times ( ()+ ), anywhere in the input ". 此结果可以转换为:“一个或多个数字( \\d+ )后跟零个或多个空格( \\s* ),一次或多次( ()+ ), 输入中的任何位置 ”。 Alternatively, we could say : "at least one digit, anywhere in the input ". 或者,我们可以说:“ 输入中的任何位置至少有一个数字”。

What you need is to replace "anywhere in the input" with "from the beginning to the end of the input". 您需要的是将“输入中的任何位置”替换为“从输入的开头到结尾”。 This is allowed by the following special characters : ^ (beginning of input) and $ (end of input). 这由以下特殊字符允许: ^ (输入开始)和$ (输入结束)。 Let's make a bunch of tests to see how they work : 让我们做一堆测试,看看它们是如何工作的:

requirement                                 regex     input   .test()
---------------------------------------------------------------------
must contain at least one digit             /\d+/     'a1a'   true  
must start with at least one digit          /^\d+/    '1a'    true  
must start with at least one digit          /^\d+/    'a1'    false 
must end with at least one digit            /\d+$/    '1a'    false 
must end with at least one digit            /\d+$/    'a1'    true  
only digits from the beginning to the end   /^\d+$/   '1a1'   false

Suggestion 建议

Only digits potentially separated by one whitespace : /^\\d+( \\d+)*$/ . 只有可能由一个空格分隔的数字: /^\\d+( \\d+)*$/

^         beginning of the input
\d+       a digit, one or more times
( \d+)*   a whitespace + same as above, zero or more times
$         end of the input

Usage example : 用法示例:

var r = /^\d+( \d+)*$/;
var isValid = r.test(' 1 ');  // false
var isValid = r.test('1 1');  // true
var isValid = r.test('1  1'); // false

More about regular expressions : http://www.javascriptkit.com/javatutors/redev.shtml . 有关正则表达式的更多信息: http//www.javascriptkit.com/javatutors/redev.shtml

try this one 试试这个

$pattern = '/^[0-9 ]+$/';

  if ( preg_match ($pattern, $text) )
  {
   echo 'allowed';
  }

Try this regular expression 试试这个正则表达式

/(\d+\s*\d*)+$/

Visualize the results here http://regex101.com/r/dE5uR8 http://regex101.com/r/dE5uR8可视化结果

I have tested it online using 我已经在线测试了它

http://regexpal.com/ http://regexpal.com/

The above regular expression will not accept empty blank spaces at the start. 上面的正则表达式在开始时不接受空的空格。 You need atleast one digit. 你需要至少一位数。 If you want to match the empty spaces at the start also change it to (\\d*\\s*\\d*)+$ which will accept empty spaces also 如果你想在开始时匹配空格,也把它改成(\\ d * \\ s * \\ d *)+ $,它也会接受空格

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM