Regular expression to match phone number?

前端 未结 5 360
庸人自扰
庸人自扰 2021-01-21 12:36

I want to match a phone number that can have letters and an optional hyphen:

  • This is valid: 333-WELL
  • This is also valid: 4URGENT
5条回答
  •  攒了一身酷
    2021-01-21 12:52

    Thank you Heath Hunnicutt for his alternation operator answer as well as showing me an example.

    Based on his advice, here's my answer:

    [A-Z0-9]{7}|[A-Z0-9][A-Z0-9-]{7}
    

    Note: I tested my regex here. (Just including this for reference)

提交回复
热议问题