简体   繁体   中英

I need to test Regex value to match correctly for my input text

My input values are:

FRKK1111122222AAAAAAAAAAA33

  • The total length is 27.
  • I need to skip first 4 characters
  • Then match 10 numbers
  • Then 11 Letters
  • Then 2 numbers from above input.

To match below condition:

Skip first 4 characters. for ex - FRKK then Check 1111122222 is number or not, Check AAAAAAAAAAA is letters or not Check 33 is number or not


I tried to break using substr and passing this string in function to match seprate values.

ischar(str){ 
    var letters = "^[a-zA-Z]*$";
    if(str.match(letters)){
      return true;
      }
      else{
        return false;
    }
  }

You can use a simple regex test

 const str = 'FRKK1111122222AAAAAAAAAAA33'; const check = /^.{4}\\d{10}[az]{11}\\d{2}$/i; console.log(check.test(str)); 


Note that

var letters = "^[a-zA-Z]*$";

is not a regex but a plain string, you're currently passing a string to match . A regex in javascript is constructed with /<regex expression here>/ , not enclosed by ' or " .

If you've already validated the string and you want to get the parts you can use this pattern. Here's the explanation.

 var re = /[\\S]{4}(\\d{10})([a-zA-Z]{11})(\\d{2})/; var str = 'FRKK1111122222AAAAAAAAAAA33'; var [str, first, second, third] = str.match(re); console.log(first, second, third); 

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