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-JAVASCRIPT Unable to find pattern

I am trying to figure how I can search for a pattern that gives me only digits or digits followed by only one letter. I know I can use /\D\g to find only digits but I dont know how to find digits with only one letter after it. The letters can only be the following:['a','A','b','B','c','C','d','D','n','N','e','E','s','S','w','W']

  const testPattern = /[A-Za-z][0-9]/
  console.log('item_10a_object10a'.pattern(testPattern))

>Solution :

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

First, you need to group the whole thing with (), and end with /g so it matches multiple groups.

If you want digits first then a number, you need to put the letter block after the numbers: ([0-9][A-Za-z])

If you want multiple digits to match, you need a + after the numbers block: [0-9]+

All together: /([0-9]+[A-Za-z])/g

For reference, \d does the same thing as [0-9], so you could do /(\d+[A-Za-z])/g instead

LPT: use regex101 to build and test regex queries

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