简体   繁体   English

正则表达式无法正常工作 - 字符串必须包含数字和由空格分隔的字符串

[英]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.

相关问题 正则表达式,字符串结尾必须包含数字 - Regex, end of string must contain number 用于空字符串或空格的正则表达式 - Regex for empty string or white space 字符串的正则表达式必须包含一个大写字母、一个小写字母、一个数字、一个没有空格的可打印 ASCI 字符 - Regex for a string that must contain an uppercase letter, a lower case letter, a digit, a printable ASCI character with no white spaces javascript: regex 必须包含一个字符串,但它也不包含某个字符串 - javascript: regex must contain a string, but it also not contain a certain string 正则表达式不匹配具有 10 个连续数字的字符串。 数字可以用空格分隔。 所有其他字符串返回匹配项 - Regex to NOT match a string with 10 consecutive digits. The digits may be separated by white space. All other string return a match 使用RegEx删除字符串中“/”后的空格 - Remove white-space after “/” in a string with RegEx 正则表达式以匹配空白之间的字符串(JavaScript) - regex to match a string among white space (JavaScript) 逗号和空格分隔的数字和连字符分隔的字符串正则表达式 - comma and space separated number and hyphen separated string regular expression 用于检查字符串的正则表达式,必须包含空格并且是字母数字 - Regular expression for checking a string, which must contain a space and be alpha numeric 逗号分隔字符串的正则表达式 - Regex for comma separated string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM