javascript - Regex for Number format Validation -


help regex match number pattern .. need accept input in below formats.for implementing validation logic need regex. tried using following

/(^(([0-9]{5,6}[,])+[0-9]{5,6})$)|(^(([0-9]{1,5}\*[,])+[0-9]{1,5}\*)$)|(^(([0-9]{5,6}[,])+[0-9]{1,5}\*)$)|(^(([0-9]{1,5}\*[,])+[0-9]{1,5})$)/ 

but not matching scenarios.

following possible inputs

  1. 1123*,2133*,123*,1*
  2. 213433,123453,123*
  3. 123333,123623,678123,12323,
  4. 1123*,123445,166788,123333,..........

conditions - number can have length 5 or 6. inputs must separated comma. if input has * (wildcard search) max length cannot greater 5. ex 123123(not allowed)*

i'm not sure understand requirements of regexp matching pattern, here's preliminary approach.

  • as understand you, numbers [to matched] can have length 5 or 6 , matching pattern should able capture amount of numbers fit description. part, assume there no asterisk characters (*) contained in number sequence.

let's assume have variable input assigned value string 213433,123453,123*. then...

input.match(/\d{5,6}(?!\*)/g); 

should capture parts of input variable described. noticed using character sets in attempt (i.e., [0-9] part); \d term accomplishes same thing.the (?!\*) portion used check 5- or 6-digit sequence not followed asterisk character. note: have escape asterisk.

  • in event input possess asterisk character, matched portion can no longer 5 characters. (i hope i'm understanding prompt correctly)! this, following regexp pattern worked (admittedly inexhaustive) 2 tests ran. note: pattern retains (i.e., matches/captures) asterisk character number sequence.

we can assume input variable here assigned string 1123*,2133*,123*,1* purposes of testing out next regexp match pattern.

input.match(/(\d+\*){1,5}/g); 

if i'm not mistaken, 2 match patterns can conditionally combined using simple or (|) operator.

input.match(/\d{5,6}(?!\*)|(\d+\*){1,5}/g); 

hopefully, helps bit. final test, let's assign input variable string 1123*,2133*,123*,1*,123333,123623,678123,12323 (i concatenated example inputs 1. , 3. provided this). then, running statement directly above returns array:

["1123*", "2133*", "123*", "1*", "123333", "123623", "678123", "12323"] 

if wanted retrieve matches without wildcard characters (*), modify expression follows:

a.match(/\d{5,6}(?!\*)|(\d+\*){1,5}/g).map(val => val.replace('*', '')); 

to get...

["1123, "2133", "123", "1", "123333", "123623", "678123", "12323"] 

Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -