简体   繁体   English

REGEX-JAVASCRIPT 无法找到模式

[英]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.我知道我可以使用/\D\g只查找数字,但我不知道如何查找后面只有一个字母的数字。 The letters can only be the following: ['a','A','b','B','c','C','d','D','n','N','e','E','s','S','w','W']字母只能如下: ['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.首先,您需要用()对整个事物进行分组,并以/g结尾,以便它匹配多个组。

If you want digits first then a number, you need to put the letter block after the numbers: ([0-9][A-Za-z])如果要先数字后数字,则需要将字母块放在数字后: ([0-9][A-Za-z])

If you want multiple digits to match, you need a + after the numbers block: [0-9]+如果要匹配多个数字,则需要在数字块后加一个+[0-9]+

All together: /([0-9]+[A-Za-z])/g加在一起: /([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作为参考, \d做同样的事情[0-9] ,所以你可以做/(\d+[A-Za-z])/g代替

LPT: use regex101 to build and test regex queries LPT:使用regex101构建和测试正则表达式查询

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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