I have get Javascript regex from this Regex link. But its match also mix pattern of MAC address
/^([0-9a-f]{1,2}[\.:-]){5}([0-9a-f]{1,2})$/i
For e.g
AA-BB.CC.DD.EE.FF
as per above regex its true but i want to match same quantifier in whole mac address. As per my requirement above mac address is wrong.
So would please help me how to match same quantifier. i.e for dot(.) find 5 instead of mix pattern same for dash(-) and colon
No you cannot get the MAC address in JavaScript, mainly because the MAC address uniquely identifies the running computer so it would be a security vulnerability.
In JavaScript, you can write RegExp patterns using simple patterns, special characters, and flags.
Using regular expressions in JavaScript. Regular expressions are used with the RegExp methods test() and exec() and with the String methods match() , replace() , search() , and split() . Executes a search for a match in a string. It returns an array of information or null on a mismatch.
One way to represent them is to form six pairs of the characters separated with a hyphen (-) or colon(:). For example, 01-23-45-67-89-AB is a valid MAC address. Another way to represent them is to form three groups of four hexadecimal digits separated by dots(.). For example, 0123.4567.
^[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
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.
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".
[0-9A-F]
is a character set that's repeated {2}
times. 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:
. Then again we have the same character set [0-9A-F]
that is repeated {2}
times.
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.
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 (i.e. a MAC address like AABBCCDDEEFF), since some vendors represent MAC addresses without a separator.
^([0-9a-fA-F]{2}[:.-]?){5}[0-9a-fA-F]{2}$
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With