[英]Regex not working correctly - String must contain a number and a string separated by a white space
I'm having some troubles with a regex expression. 我有一些正则表达式的麻烦。 I need the fourth field to be enabled when the third one is filled with a number and a string separated by a white space, but for some reason I can't make it work.
当第三个字段填充数字和字符串由空格分隔时,我需要启用第四个字段,但由于某种原因,我无法使其工作。
This is the pattern: \\d+ *?.*? .*
这是模式:
\\d+ *?.*? .*
\\d+ *?.*? .*
Here's a fiddle : 这是一个小提琴 :
function validateStreetNumber(variable){ var reg=new RegExp('/\\d+ *?.*? .*'); console.log(reg); var compare=reg.test(variable); return compare; } $(document).ready(function(){ $('#state').prop('disabled',true); $('#address').prop('disabled',true); $('#number').prop('disabled',true); $('#city').on("input",function(){ var value=$(this).val(); if(value==''){ $('#state').prop('disabled',true); $('#address').prop('disabled',true); $('#number').prop('disabled',true); } else{ $('#state').prop('disabled',false); } }); $('#state').on("change",function(){ var value=$(this).val(); if(value=='select'){ $('#address').prop('disabled',true); $('#number').prop('disabled',true); } else{ $('#address').prop('disabled',false); } }); $('#address').on("input",function(){ var value=$(this).val(); var exp=validateStreetNumber(value); if(!exp){ $('#number').prop('disabled',true); } else if(exp){ $('#number').prop('disabled',false); } }); });
input, select{ width: 200px; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form action="#" id="test" name="test"> <input type="text" id="city" name="city" maxlength="100" /><br> <select id="state" name="state"> <option value="select">Select</option> <option value="aragua">Aragua</option> <option value="carabobo">Carabobo</option> <option value="miranda">Miranda</option> <option value="zulia">Zulia</option> <option value="bolivar">Bolívar</option> </select><br> <input type="text" id="address" name="address" maxlength="100"/><br> <input type="text" id="number" name="number" maxlength="8"/><br> </form>
(Sorry for my english) (对不起我的英语不好)
change 更改
var reg=new RegExp('/\d+ *?.*? .*');
to 至
var reg=new RegExp('\d+ *?.*? .*');
There is no need to put the / when your using the RegEx object constructor. 使用RegEx对象构造函数时无需使用/。
Also if you just want any number separated by a space and followed by a string of any character (including space) you should simply use: \\d+ .* 此外,如果您只想要任何以空格分隔的数字,后跟任意字符(包括空格)的字符串,您只需使用:\\ d +。*
Your regex - new RegExp('/\\d+ *?.*? .*')
- evaluates to /\\/d+ *?.*? .*/
你的正则表达式 -
new RegExp('/\\d+ *?.*? .*')
- 评估为/\\/d+ *?.*? .*/
/\\/d+ *?.*? .*/
regex: a /
, followed with one or more d
letters, a space zero or more times, any char zero or more times, a space and any 0+ chars. /\\/d+ *?.*? .*/
正则表达式:a /
,后跟一个或多个d
字母,空格零次或多次,任何char零次或多次,空格和任何0+字符。 The problem is that you have /
at the start and an improperly escaped d
(you need 2 backslashes in the constructor notation). 问题是你有
/
在开始时和一个不正确的转义d
(你在构造函数表示法中需要2个反斜杠)。
You need to use the following regex: 您需要使用以下正则表达式:
var reg=/^\d+\s/;
It checks if a string starts with 1+ digits and a whitespace after them. 它检查字符串是否以1+位开头,后面是空格。
There is no point adding .*
or .*?
没有必要补充
.*
或.*?
since you are only interested in what is at the beginning of the string. 因为你只对字符串开头的内容感兴趣。
Details : 细节 :
^
- string start ^
- 字符串开始 \\d+
- 1 or more digits \\d+
- 1位或更多位数 \\s
- a whitespace. \\s
- 一个空白。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.