Regular expression for no more than two repeated letters/digits

前端 未结 5 1710
北恋
北恋 2020-12-03 07:23

I have a requirement to handle a regular expression for no more than two of the same letters/digits in an XSL file.

  • no space
  • does not support special
5条回答
  •  误落风尘
    2020-12-03 08:10

    Does this one work for you?

    /(\b(?:([A-Za-z0-9])(?!\2{2}))+\b)/
    

    Try it out:

    var regex = new RegExp(/(\b(?:([A-Za-z0-9])(?!\2{2}))+\b)/)
    var tests = ['A1D3E', 'AAAA', 'AABAA', 'abccddeeff', 'abbbc', '1234']
    
    for(test in tests) {
       console.log(tests[test] + ' - ' + Boolean(tests[test].match(regex)))
    }
    

    Will output:

    A1D3E - true
    AAAA - false
    AABAA - true
    abccddeeff - true
    abbbc - false
    1234 - true
    

提交回复
热议问题