Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MAC address regex for JavaScript

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

like image 752
Hkachhia Avatar asked Mar 24 '15 09:03

Hkachhia


People also ask

Can you get MAC address using JavaScript?

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.

Does regex work in JavaScript?

In JavaScript, you can write RegExp patterns using simple patterns, special characters, and flags.

What regex does JavaScript use?

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.

How do you validate a MAC address?

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.


Video Answer


4 Answers

^[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

like image 163
vks Avatar answered Nov 11 '22 06:11

vks


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
like image 30
Avinash Raj Avatar answered Nov 11 '22 08:11

Avinash Raj


\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.

like image 33
Selim Ekizoglu Avatar answered Nov 11 '22 08:11

Selim Ekizoglu


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}$
like image 30
diazdeteran Avatar answered Nov 11 '22 07:11

diazdeteran