简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM