简体   繁体   中英

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+ *?.*? .*
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.

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+ .*

Your regex - new RegExp('/\\d+ *?.*? .*') - evaluates to /\\/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. The problem is that you have / at the start and an improperly escaped d (you need 2 backslashes in the constructor notation).

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.

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
  • \\s - a whitespace.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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