Home > Back-end >  Regex in innerHTML to check for 2 numbers and one letter
Regex in innerHTML to check for 2 numbers and one letter

Time:06-27

I have an if statement in my Chrome extension that checks a page for listings to see if any match something in particular, such as:

if(matches[i].innerHTML.includes("12B"){

and that works fine. All said and done, the full return of matches[i].innerHTML will always be something like:

"12B Example"

So I'm primary interested in the initial 2 numbers, then a specific letter. So I want to add an additional check in the if statement to see if it matches any 2 numbers, then followed by the letter B specifically. Then after the B, I don't care what is there, I want it to be considered a match.

I tried the following:

if(matches[i].innerHTML.includes(/^[0-9]{2}[B]{1}/){

and that doesn't seem to be working. Is that sort of expression valid in setting?

I also tried: ([0-9]{2}[B]{1}) and similar. I looked here and in the documentation but wasn't able to find anything that gets me any further.

CodePudding user response:

This regex will evaluate that there is 2 numbers and the B letter in capital at the beginning of the string, after that it will check if there's any other values or if it's empty.

if(matches[i].innerHTML.match(/^[0-9]{2}B.*$/)

Examples:

  • 12B
  • 01B46598
  • 99B46zAw%$
  • Related