Regex to match a 2-digit number (to validate Credit/Debit Card Issue number)

前端 未结 3 1015
青春惊慌失措
青春惊慌失措 2021-02-02 08:46

I would like to use regex to match a string of exactly 2 characters, and both of those characters have to be between 0 and 9. The string to match against would be coming from a

相关标签:
3条回答
  • 2021-02-02 09:26

    Something like this would work

    /^\d{2}$/
    
    0 讨论(0)
  • 2021-02-02 09:32

    You can use the start (^) and end ($) of line indicators:

    ^[0-9]{2}$
    

    Some language also have functions that allows you to match against an entire string, where-as you were using a find function. Matching against the entire string will make your regex work as an alternative to the above. The above regex will also work, but the ^ and $ will be redundant.

    0 讨论(0)
  • 2021-02-02 09:35

    You need to use anchors to match the beginning of the string ^ and the end of the string $

    ^[0-9]{2}$
    
    0 讨论(0)
提交回复
热议问题