Javascript REGEX - 精确文本和数字

时间:2016-11-20 08:48:34

标签: javascript regex

我正在寻找与文本'PDR'或'pdr'完全匹配的正则表达式和8位数,所以总共11位数,(3文本+8位数)

  

pdr16120008 - TRUE

     

PDR16120009 -TRUE

     

rdp16120001-错误

2 个答案:

答案 0 :(得分:0)

Regex101

^(pdr|PDR)\d{8}$

Regular expression visualization

Debuggex Demo

描述

^ asserts position at start of a line
1st Capturing Group (pdr|PDR)
    1st Alternative pdr
        pdr matches the characters pdr literally (case sensitive)
    2nd Alternative PDR
        PDR matches the characters PDR literally (case sensitive)
\d{8} matches a digit (equal to [0-9])
    {8} Quantifier — Matches exactly 8 times
$ asserts position at the end of a line

答案 1 :(得分:0)

正则表达并不难:

  1. 使用pdr
  2. 在一开始就匹配^
  3. 使用字符类和量词完全匹配 8 digits
  4. 使用i修饰符以匹配不区分大小写。
  5. const REGEX = /^pdr[0-9]{8}$/i;
    
    let valids = ['pdr16120008', 'PDR16120009', 'rdp16120001']
      .filter(input => REGEX.test(input))
    ;
    
    console.log({valids});