Fastest method for testing a fixed phone number pattern

后端 未结 6 1984
孤街浪徒
孤街浪徒 2021-01-05 20:11

So, the challenge is that we are trying to detect if a string matches a fixed phone number pattern, this is a simple string pattern.

The pattern is:

         


        
6条回答
  •  Happy的楠姐
    2021-01-05 20:35

    A slight variation of one of the other answers, but I believe the built-in character class should be slightly faster than a custom one:

    return /^\d{3}-\d{3}-\d{4}$/.test(phoneNumber);
    

提交回复
热议问题