Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 带有空格和+;在前面签名_Javascript_Regex - Fatal编程技术网

Javascript 带有空格和+;在前面签名

Javascript 带有空格和+;在前面签名,javascript,regex,Javascript,Regex,如果我只接受数字,那么我将使用这个正则表达式 ^[0-9]*$ 但这里的问题是这些数字 +100 没有被捕获,我的正则表达式将显示它无效 用户只需键入数字,但中间只允许有一个空格,并且Begging处的+号应为可选 因此,可以接受的是: +1 11 1 1 11 or 1 11 1 1 11 +1 11 1 1 11 or 1 11 1 1 11 不可接受的是: +1 11 1 1 11 or 1 11 1 1 11 +1 11 1 1 11 or 1 11

如果我只接受数字,那么我将使用这个正则表达式

^[0-9]*$
但这里的问题是这些数字

+100

没有被捕获,我的正则表达式将显示它无效

用户只需键入数字,但中间只允许有一个空格,并且Begging处的+号应为可选

因此,可以接受的是:

+1 11 1 1 11 
or
1 11 1 1 11 
+1    11 1 1 11
or
1 11     1 1 11 
不可接受的是:

+1 11 1 1 11 
or
1 11 1 1 11 
+1    11 1 1 11
or
1 11     1 1 11 

请提供帮助

您可以尝试使用此正则表达式模式:

^\+?\d+(?:[]?\d+)*$
示例脚本:

console.log(/^\+?\d+(?:[]?\d+*$/.test('+11'));//符合事实的
console.log(/^\+?\d+(?:[]?\d+*$/.test('1111'));//符合事实的
console.log(/^\+?\d+(?:[]?\d+*$/.test(“+11”);//错误的
console.log(/^\+?\d+(?:[]?\d+*$/.test('1111'));//错误
关闭

/^[0-9]{1,}$/g


^ = start/first character
[0-9] = Select only number 0-9 but match it once,
{1,} = Match it one or more times,
$ = look no further, so cut all spaces, letters or non matches out!
甚至

/^[0-9]+$/g
甚至(首选)

除了数字以外,你不应该再匹配任何东西

函数CheckInt(inputNum){
if(inputNum.toString().match(/^-?[1-9]\d*\.?(\d+)$/g)){
log(`${inputNum}是一个数字(INT)`);
}否则{
log(`${inputNum}不是一个数字(INT)`);
}
}
CheckInt(“a”);
CheckInt(“b”);
CheckInt(“c”);
CheckInt(“102 020”);
CheckInt(“102-1029”);
CheckInt(5400);
CheckInt(-2);
CheckInt(20);

CheckInt(2042992540)
^\+?\d+(?:\d+)$
regex101.com是测试regexs:)数字末尾的空格是否应该匹配?不匹配
+1
即使它应该匹配为什么
?:
?它在没有它的情况下工作@NinoFiliu The
?:
只是关闭了捕获组,因为我们不想使用它。谢谢Tim。它起作用了