Javascript conditional regular expression if-then-else

前端 未结 3 1853
一生所求
一生所求 2020-12-06 02:20

I\'m trying to limit the entries to a specific format.

If the entry has 5500 or 5100 such as 01\\01-5500-000-00 then I want to have this:

^[0-9]{2,}\         


        
3条回答
  •  生来不讨喜
    2020-12-06 02:31

    Replace this part of the pattern [0-9]{4} with the literal values for 5100/5500:

    /^\d{2,}\\\d{2}-(?:5[15]00-\d{3}-|(?!5[15]00)\d{4}-\d{3}-\d{2})$/
    
    • [0-9] is the same thing as \d in JavaScript
    • For the else part, use the negative lookahead (?!5[15]00)\d{4} to guarantee it's not 5100/5500.

提交回复
热议问题