Mac address regex

Mac address

Mac address is a unique identifier assigned to network interface controllers like WiFi routers, Ethernet controllers, etc. It has a format of six groups of 2 hexadecimal digits separated by dash or colon (e.g. 00:00:5e:00:53:af). Mac address regular expression can be used to validate that a certain string contains mac address or extract mac address from a given string.

Simple Mac address regex (IEEE 802)

Below is a simple mac address regex that supports dashes or colons as separators:

/^(?:[0-9A-Fa-f]{2}[:-]){5}(?:[0-9A-Fa-f]{2})$/

Test it!
/^(?:[0-9A-Fa-f]{2}[:-]){5}(?:[0-9A-Fa-f]{2})$/

True

False

Enter a text in the input above to see the result

Example code in Javascript:

var macRegex = /^(?:[0-9A-Fa-f]{2}[:-]){5}(?:[0-9A-Fa-f]{2})$/;
// Validate mac address
macRegex.test('00:00:5e:00:53:af'); // Returns true

// Extract mac address from a string
var macRegexG = /(?:[0-9A-Fa-f]{2}[:-]){5}(?:[0-9A-Fa-f]{2})/g;
'Unknown error in node 00:00:5e:00:53:af. Terminating.'.match(macRegexG); // returns ['00:00:5e:00:53:af']

Test it!

True

False

Enter a text in the input above to see the result

Test it!

True

False

Enter a text in the input above to see the result