Match and validate MAC (Media Access Control) addresses in colon-separated, dash-separated, and dot-separated formats using regex.
/\b(?:[0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}\b/g| Token | Meaning |
|---|---|
\b | Word boundary to prevent partial matches |
[0-9A-Fa-f]{2} | Exactly 2 hexadecimal characters (one octet) |
[:-] | Colon or dash separator between octets |
{5} | Repeat the octet-separator group 5 times |
[0-9A-Fa-f]{2} | Final octet (no trailing separator) |
00:1A:2B:3C:4D:5EMatchAA:BB:CC:DD:EE:FFMatch00-1A-2B-3C-4D-5EMatchff:ff:ff:ff:ff:ffMatch00:1A:2B:3C:4DNo matchGG:HH:II:JJ:KK:LLNo match001A2B3C4D5ENo matchconst macRegex = /\b(?:[0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}\b/g;
const text = "Devices: 00:1A:2B:3C:4D:5E and AA-BB-CC-DD-EE-FF";
const matches = text.match(macRegex);
console.log(matches); // ["00:1A:2B:3C:4D:5E", "AA-BB-CC-DD-EE-FF"]import re
mac_regex = r'\b(?:[0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}\b'
text = "Devices: 00:1A:2B:3C:4D:5E and AA-BB-CC-DD-EE-FF"
matches = re.findall(mac_regex, text)
print(matches) # ['00:1A:2B:3C:4D:5E', 'AA-BB-CC-DD-EE-FF']Want to test this pattern with your own data?
Try this pattern in Regex Tester →