【PHP、JS】MAC地址正则匹配
最近写一个功能,用于随机生成MAC地址,了解了一下MAC的组成规则。顺便写一下怎样使用正则去匹配MAC
正则([A-Fa-f\d]{2}[:\s-.]{1}){5}[A-Fa-f\d]{2}
匹配以下四种类型
1. FF:FF:FF:FF:FF:FF
2. FF.FF.FF.FF.FF.FF
3. FF-FF-FF-FF-FF-FF
4. FF FF FF FF FF FF
一、JS语言示例
var mac = "02:08:10:0A:8C:6F"
var reg = /([A-Fa-f\d]{2}[:\s-.]{1}){5}[A-Fa-f\d]{2}/;
if(reg.test(mac)){
alert('正确');
}
二、php语言示例
$mac = "02:08:10:0A:8C:6F";
if(preg_match("/([A-Fa-f\d]{2}[:\s-.]{1}){5}[A-Fa-f\d]{2}/",$mac)){
echo "正确!";
}
其它
提供一款在线工具,随机生成mac地址
https://tl.beer/randmac.html