简体   繁体   English

JavaScript 的 MAC 地址正则表达式

[英]MAC address regex for JavaScript

I have get Javascript regex from this Regex link.我从这个Regex链接获得了 Javascript regex。 But its match also mix pattern of MAC address但它的匹配也混合了 MAC 地址的模式

/^([0-9a-f]{1,2}[\.:-]){5}([0-9a-f]{1,2})$/i

For eg例如

AA-BB.CC.DD.EE.FF  

as per above regex its true but i want to match same quantifier in whole mac address.按照上面的正则表达式它是真的,但我想在整个 mac 地址中匹配相同的量词。 As per my requirement above mac address is wrong.根据我的要求,上面的 mac 地址是错误的。

So would please help me how to match same quantifier.所以请帮助我如何匹配相同的量词。 ie for dot(.) find 5 instead of mix pattern same for dash(-) and colon即对于点(。)找到 5 而不是混合模式相同的破折号(-)和冒号

^[0-9a-f]{1,2}([\.:-])(?:[0-9a-f]{1,2}\1){4}[0-9a-f]{1,2}$

Try this.See demo.试试这个。看演示。

https://regex101.com/r/tJ2mW5/12 https://regex101.com/r/tJ2mW5/12

Change your regex like below.像下面一样改变你的正则表达式。

^[0-9a-f]{1,2}([\.:-])[0-9a-f]{1,2}(?:\1[0-9a-f]{1,2}){4}$

case-insensitive modifier i heps to do a case-insensitive match.不区分大小写的修饰符i想进行不区分大小写的匹配。

DEMO演示

> /^[0-9a-f]{1,2}([.:-])[0-9a-f]{1,2}(?:\1[0-9a-f]{1,2}){4}$/i.test('AA-BB.CC.DD.EE.FF')
false
> /^[0-9a-f]{1,2}([.:-])[0-9a-f]{1,2}(?:\1[0-9a-f]{1,2}){4}$/i.test('AA.BB.CC.DD.EE.FF')
true
\b([0-9A-F]{2}[:-]){5}([0-9A-F]){2}\b

\\b is an anchor like the ^ and $ that matches at a position that is called a "word boundary". \\b是像^$一样的锚点,它在称为“单词边界”的位置匹配。

[0-9A-F] is a character set that's repeated {2} times. [0-9A-F]是重复{2}次的字符集。 After the character set there's : or - and the grouping ([0-9A-F]{2}[:-]) is repeated {5} times which gives us ex: 2F:3D:A9:B6:3F: .在字符集之后有:-并且分组([0-9A-F]{2}[:-])重复{5}次,这给了我们例如: 2F:3D:A9:B6:3F: Then again we have the same character set [0-9A-F] that is repeated {2} times.然后我们有相同的字符集[0-9A-F]重复{2}次。

The answers provided are fine, but I would add lowercase letters and the dot (.) separator.提供的答案很好,但我会添加小写字母和点 (.) 分隔符。 Also, MAC addresses with just one letter or number in every position are invalid.此外,每个位置只有一个字母或数字的 MAC 地址是无效的。

Here's a regular expression that matches numbers, capital and lowercase letters, checks for two characters in every position, and allows a semicolon (:), dash (-) or dot (.) as a separator.这是一个匹配数字、大写和小写字母的正则表达式,检查每个位置是否有两个字符,并允许使用分号 (:)、破折号 (-) 或点 (.) 作为分隔符。

^([0-9a-fA-F]{2}[:.-]){5}[0-9a-fA-F]{2}$ 

The regular expression below will also match a MAC address without a delimiter (ie a MAC address like AABBCCDDEEFF), since some vendors represent MAC addresses without a separator.下面的正则表达式也将匹配没有分隔符的 MAC 地址(即像 AABBCCDDEEFF 这样的 MAC 地址),因为一些供应商表示没有分隔符的 MAC 地址。

^([0-9a-fA-F]{2}[:.-]?){5}[0-9a-fA-F]{2}$

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM