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))
CodePudding user response:
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