Regex pattern that matches any number include 1-9 except 2

前端 未结 4 1972
说谎
说谎 2021-02-19 08:15

I need a regular expression pattern that matches any number including 1-9 numbers except 2?

My attempt:

([1-9][^2])

But this doesn\'t w

相关标签:
4条回答
  • 2021-02-19 08:21

    Another way to do it:

    /[^\D2]/
    

    Which means, not a non-digit or 2.

    0 讨论(0)
  • 2021-02-19 08:25

    You can match the range of numbers before and after two with [0-13-9], like this:

    "4526".match(/[0-13-9]+/)
    ["45"]
    "029".match(/[0-13-9]+/)
    ["0"]
    "09218".match(/[0-13-9]+/)
    ["09"]
    
    0 讨论(0)
  • 2021-02-19 08:31

    This RegExp works: /([013-9])/

    0 讨论(0)
  • 2021-02-19 08:47

    Or this is also the correct answer.

    /(?!2)\d/

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