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

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:

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

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));
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