RegExp range of number (1 to 36)

后端 未结 7 1467
臣服心动
臣服心动 2020-12-06 00:32

I searched a lot and can\'t find the solution for this RegExp (I have to say I\'m not very experienced in Reg. Expressions).

I would like to test a number between 1

相关标签:
7条回答
  • 2020-12-06 00:49

    Try this:

    ^[1-9]$|^[1-2][0-9]$|^3[0-6]$
    

    (All 1 digit numbers between 1 and 9, all 1x and 2x numbers, and 3x numbers from 30 to 36).

    0 讨论(0)
  • 2020-12-06 00:49

    Try ^[1-9]$|^[1-2]\d$|^3[0-6]$

    0 讨论(0)
  • 2020-12-06 00:52

    I'm not sure why all of the answers to this repeat the mistake of adding boundaries (^ and $) before and after each condition. But you only need to do:

    ^[1-9]|[1-2][0-9]|3[0-6]$
    

    I also created a JavaScript/Node.js library, to-regex-range, to simplify creating ranges like this.

    0 讨论(0)
  • 2020-12-06 00:52

    Try this:

    /^[1-9]$|^[1-2]\d$|^3[0-6]$/
    

    DEMO

    0 讨论(0)
  • 2020-12-06 00:53

    You know about \d, right?

    ^([1-9]|[12]\d|3[0-6])$
    

    Try this in console:

    function test() {
        for(var i = 0; i < 100; i++) {
            if (/^([1-9]|[12]\d|3[0-6])$/.test(i.toString()) != (i >= 1 && i <=36)) {
                document.write(i + "fail");
            }
                    else
                    document.write(i + "pass");
            document.write("<br/>");
        }
    }
    
    0 讨论(0)
  • 2020-12-06 00:55

    Purely for academic reasons, I'll add a unique and accurate pattern.

    /^(3[0-6]?|[12]\d?|[4-9])$/
    

    There are three branches in the pattern:

    1. The first matches: 3, 30, 31, 32, 33, 34, 35, 36
    2. The second matches: 1, 2, 10-19, 20-29
    3. The third matches: 4, 5, 6, 7, 8, 9

    If there is an efficiency advantage (not that this task is likely to be a major resource drain -- unless you are doing thousands of these evaluations) in my pattern, it will come down to the fact that there are no redundant checks in the pattern.

    It may not make any difference to the regex engine, but I've ordered my branches based on the ones that take the least "effort" to evaluate (instead of the natural sequence of numbers).

    Harpo's pattern is as brief as the pattern can be built, but [123] are first-digit matches that are satisfied by the multiple branches.

    @MichaelHoffmann's and @jonschlinkert's patterns are not correct because they fail to distribute the necessary anchors to each branch. This is concisely achieved by wrapping all branches in a capture group, but as @ninjalj, @qwertymk, and @amit_g demonstrated, it is just as accurate to apply the anchors to each branch.

    let txt = '<table border=1 cellpadding=4><tr><th>Number</th><th>Harpo</th><th>Michael Hoffmann</th><th>ninjalj</th><th>jonschlinkert</th><th>qwertymk</th><th>amit_g</th><th>mickmackusa</th></tr>',
        str,
        i;
        
    for (i = 0; i <= 40; ++i) {
      str = '' + i;
      txt += '<tr><td>' + i;
      txt += '</td><td>'  + /^([1-9]|[12]\d|3[0-6])$/.test(str);
      txt += '</td><td>' + /^[0-9]|[0-2][0-9]|3[0-6]$/.test(str);
      txt += '</td><td>' + /^[1-9]$|^[1-2][0-9]$|^3[0-6]$/.test(str);
      txt += '</td><td>' + /^[1-9]|[1-2][0-9]|3[0-6]$/.test(str);
      txt += '</td><td>' + /^[1-9]$|^[1-2]\d$|^3[0-6]$/.test(str);
      txt += '</td><td>' + /^[1-9]$|^[1-2]\d$|^3[0-6]$/.test(str);
      txt += '</td><td>' + /^(3[0-6]?|[12]\d?|[4-9])$/.test(str);
      txt += '</td></tr>';
    }
    txt = txt + '</table>';
    
    document.getElementById('test').innerHTML = txt;
    <div id="facts">Correct Pattern Designers: Harpo, ninjalj, qwertymk, amit_g, mickmackussa<br>Incorrect Patterns: Michael Hoffmann, jonschlinkert <br></div>
    <div id="test"></div>

    0 讨论(0)
提交回复
热议问题