How to check if a string matches an exact pattern?

I have an array of strings in JavaScript

var myArray=["23y56","45yu45","34h78","32H41"];

What I want is to extract those values that are of lenght 5 filtered by the following:

myArray[i].length===5 // result will be "23y56" "34h78" "32H41"

Now what I want next is to get exactly those values that have exactly 2 Numbers at start and end, with the letter "h" or "H" in middle.
desired Output:

myArray=["34h78","32H41"]

How would I achieve this?
Thanks in Advance.

>Solution :

You could use a filter() and a regular expression to match on a regular expression.

myArray.filter(s => /\d{2}[hH]\d{2}/.test(s));

Leave a Reply