Mac地址上的Javascript正则表达式

时间:2013-10-30 03:53:48

标签: javascript regex mac-address

假设我有一个mac地址列表,例如:“00:11:22:33:44:55,11:22:33:44:55:66,22:33:44:55:66:77 “

我想对该列表进行正则表达式检查。

var re = /(([A-Fa-f0-9]{2}[:]){5}[A-Fa-f0-9]{2}[,]?)+/g

然而,它不起作用。以下是输入测试。

var t1 = "11:22:33:44:55:66";
var t2 = t1 + ",12:22:33:44:55:66";
var t3 = t1 + ",11asdfasdf:22:33:44:55:66";
var t4 = t1 + ",haha";
var t5 = t1 + ",";
var t6 = "123123123123";
var t7 = t1 + ",33:44:55:66:77:88:";
var t8 = t1 + ",33:44:55:66:77:88asdfasdfasdfasdfasdfasd";
var t9 = t1 + ",dfasdfasdfasdfasdfasd";
var t10 = t2 + ",12:33:44:55:66:77";
var t11 = t2 + ",wahaa";

console.log("t1: [" + t1 + "] " + re.test(t1));
console.log("t2: [" + t2 + "] " + re.test(t2));
console.log("t3: [" + t3 + "] " + re.test(t3));
console.log("t4: [" + t4 + "] " + re.test(t4));
console.log("t5: [" + t5 + "] " + re.test(t5));
console.log("t6: [" + t6 + "] " + re.test(t6));
console.log("t7: [" + t7 + "] " + re.test(t7));
console.log("t8: [" + t8 + "] " + re.test(t8));
console.log("t9: [" + t9 + "] " + re.test(t9));
console.log("t10: [" + t10 + "] " + re.test(t10));
console.log("t11: [" + t11 + "] " + re.test(t11));   

结果

t1: [11:22:33:44:55:66] true 
t2: [11:22:33:44:55:66,12:22:33:44:55:66] true 
t3: [11:22:33:44:55:66,11asdfasdf:22:33:44:55:66] false 
t4: [11:22:33:44:55:66,haha] true 
t5: [11:22:33:44:55:66,] false 
t6: [123123123123] false 
t7: [11:22:33:44:55:66,33:44:55:66:77:88:] true 
t8: [11:22:33:44:55:66,33:44:55:66:77:88asdfasdfasdfasdfasdfasd] false 
t9: [11:22:33:44:55:66,dfasdfasdfasdfasdfasd] true 
t10: [11:22:33:44:55:66,12:22:33:44:55:66,12:33:44:55:66:77] true
t11: [11:22:33:44:55:66,12:22:33:44:55:66,wahaa] false

任何人都可以告诉我为什么t7,t9和t4会返回true? 此外,当我在chrome控制台上键入re.test(t11)时,我发现结果将在true和false之间切换。谁知道为什么?

2 个答案:

答案 0 :(得分:5)

由于您的正则表达式已经设计为匹配字符串中的所有mac地址(而不是一次只匹配一个),因此您可以通过删除g标志来修复此错误。另外,为防止仅匹配字符串的一部分,您需要在正则表达式的开头和结尾包含^$个字符。

var re = /^(([A-Fa-f0-9]{2}[:]){5}[A-Fa-f0-9]{2}[,]?)+$/

因为您正在使用全局正则表达式(由于最后的g),正则表达式会保持关于其最后一次匹配的状态,这将影响将来的匹配。

具体来说,存储在re变量中的RegExp对象的lastIndex更新为指向每次匹配后的第一个字符。即使您匹配新字符串,也会从该索引开始执行将来的匹配。一旦匹配失败,lastIndex将重置为0

从图形上看,这是匹配的,每次匹配后re.lastIndex点的位置:

    t1:  11:22:33:44:55:66
                          ^
    t2:  11:22:33:44:55:66,12:22:33:44:55:66
                                            ^
    t3:  11:22:33:44:55:66,11asdfasdf:22:33:44:55:66 No match!
         ^ Since there was no match, lastIndex is reset to 0.
    t4:  11:22:33:44:55:66,haha
                           ^
    t5:  11:22:33:44:55:66, No match!
         ^ 
    t6:  123123123123 No match!
         ^
    t7:  11:22:33:44:55:66,33:44:55:66:77:88:
                                            ^
    t8:  11:22:33:44:55:66,33:44:55:66:77:88asdfasdfasdfasdfasdfasd No match!
         ^
    t9:  11:22:33:44:55:66,dfasdfasdfasdfasdfasd
                           ^
    t10: 11:22:33:44:55:66,12:22:33:44:55:66,12:33:44:55:66:77
                                                              ^
    t11: 11:22:33:44:55:66,12:22:33:44:55:66,wahaa No match!
         ^

答案 1 :(得分:3)

您需要使用行^行锚点和$行结束锚点。您的测试仅验证字符串是否包含正确匹配,而不是验证整个字符串是否正确。

var str = 'This is a sentence';
var re = /e/;
re.test(str); // returns true

var re2 = /^e$/;
re2.test(str); // returns false
re2.test("e"); // returns true

为了得到最准确的答案,我建议:

  1. 拆分“,”str.split(",")以获取候选MAC地址数组
  2. 将Array的每个元素与以下修改的正则表达式匹配: var re = /^([0-9A-F]{2}[:-]){5}([0-9A-F]{2})$/i 我使用i标志对正则表达式不区分大小写,并且还添加了-,因为有时MAC地址是这样编写的:3D-F2-C9-A6-B3-4F