Fastest method for testing a fixed phone number pattern

后端 未结 6 2020
孤街浪徒
孤街浪徒 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条回答
  •  南笙
    南笙 (楼主)
    2021-01-05 20:44

    even faster than before:

    function tecjam5(pattern) {
        var c;
        return !(pattern.length != 12 ||
        !(((c=pattern.charCodeAt(2))>>3) == 6 || (c>>1) == 28) ||
        !(((c=pattern.charCodeAt(4))>>3) == 6 || (c>>1) == 28) ||
        !(((c=pattern.charCodeAt(11))>>1) == 28 || (c>>3) == 6) ||
        !(((c=pattern.charCodeAt(0))>>3) == 6 || (c>>1) == 28) ||
        !(((c=pattern.charCodeAt(1))>>3) == 6 || (c>>1) == 28) ||
        !(((c=pattern.charCodeAt(5))>>3) == 6 || (c>>1) == 28) ||
        !(((c=pattern.charCodeAt(6))>>3) == 6 || (c>>1) == 28) ||
        !(((c=pattern.charCodeAt(8))>>3) == 6 || (c>>1) == 28) ||
        !(((c=pattern.charCodeAt(9))>>3) == 6 || (c>>1) == 28) ||
        !(((c=pattern.charCodeAt(10))>>1) == 28 || (c>>3) == 6) ||
        pattern.charAt(3) != '-' || pattern.charAt(7) != '-');
    }
    

    (short: every number < 8 only need compared once)

提交回复
热议问题