Regex for MAC Addresses
A MAC address is six bytes written as hex pairs. This pattern accepts colon- or hyphen-separated forms in either case. It doesn't insist that all five separators match, so a mixed address like 00:1A-2B:3C:4D:5E passes — the first variation fixes that with a backreference.
/^(?:[0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}$/How it works, token by token
| Token | Meaning |
|---|---|
| ^ | start of string |
| (?:[0-9A-Fa-f]{2}[:-]){5} | five hex pairs, each followed by a colon or a hyphen |
| [0-9A-Fa-f]{2} | the sixth pair, with no separator after it |
| $ | end of string |
What it matches
00:1A:2B:3C:4D:5E00-1a-2b-3c-4d-5eFF:FF:FF:FF:FF:FF00:1A:2B:3C:4D001A2B3C4D5E00:1G:2B:3C:4D:5ETry it live
One candidate per line — the m flag makes the ^ and $ anchors apply to each line. Edit anything; it runs in your browser.
Variations
^[0-9A-Fa-f]{2}([:-])(?:[0-9A-Fa-f]{2}\1){4}[0-9A-Fa-f]{2}$the separator must be the same throughout (backreference \1)^(?:[0-9A-Fa-f]{4}\.){2}[0-9A-Fa-f]{4}$Cisco dotted form: 001a.2b3c.4d5eLanguage notes
- Backreferences like \1 work in JavaScript, Python, PCRE and Java. Go's RE2 engine doesn't support them — use the base pattern there and compare the separators in code.
Frequently asked questions
Should I accept MAC addresses without separators?
Many tools print them bare (001A2B3C4D5E). Normalise input first — strip colons, hyphens and dots — then check for exactly 12 hex digits with ^[0-9A-Fa-f]{12}$.
Does this check that the MAC address is real?
No. Any 12 hex digits pass, including the broadcast address (FF:FF:FF:FF:FF:FF) and locally administered addresses. Use it for format validation only.
Building something custom? The regex tester gives you live match highlighting for any pattern.