Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Regex in innerHTML to check for 2 numbers and one letter

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"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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%$
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading