RegExp range of number (1 to 36)

后端 未结 7 1468
臣服心动
臣服心动 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 01:14

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

    Here's a breakdown of it:


    [0-9] = any digit from 0-9
    | = OR
    [0-2][0-9] = '1' or '2', followed by any digit from 0-9
    | = OR
    3[0-6] = '3', followed by any digit from 0-6.

    As @mu is too short said, using an integer comparison would be a lot easier, and more efficient. Here's an example function:

    function IsInRange(number)
    {
        return number > 0 && number < 37;
    }
    
    0 讨论(0)
提交回复
热议问题