Regex to Match MAC Addresses
Validate MAC addresses using this regex pattern. Matches both colon-separated and hyphen-separated hexadecimal byte format. Free regex tool.
Regular Expression
/([0-9a-fA-F]{2}[:-]){5}[0-9a-fA-F]{2}/gi
Token Breakdown
| Token | Description |
|---|---|
| ( | Start of capturing group |
| [0-9a-fA-F] | Character class — matches any one of: 0-9a-fA-F |
| {2} | Matches exactly 2 times |
| [:-] | Character class — matches any one of: :- |
| ) | End of group |
| {5} | Matches exactly 5 times |
| [0-9a-fA-F] | Character class — matches any one of: 0-9a-fA-F |
| {2} | Matches exactly 2 times |
Detailed Explanation
This regex matches MAC (Media Access Control) addresses in their standard notation. Here is the token-by-token breakdown:
( — Opens a capturing group for the first five pairs of hex digits, each followed by a separator.
[0-9a-fA-F]{2} — A character class matching exactly two hexadecimal digits. It includes digits 0-9, lowercase a-f, and uppercase A-F. Each pair represents one byte of the six-byte MAC address.
[:-] — A character class matching either a colon or a hyphen. MAC addresses are commonly written with either separator style: AA:BB:CC:DD:EE:FF or AA-BB-CC-DD-EE-FF. This class accepts both formats.
){5} — Closes the capturing group and repeats it exactly five times. This matches the first five byte-pairs, each with a trailing separator.
[0-9a-fA-F]{2} — Matches the sixth and final byte-pair of two hexadecimal digits, without a trailing separator.
The g flag enables global matching to find all MAC addresses in the text, and the i flag makes the hex letters case-insensitive. Note that this pattern allows mixed separators (e.g., AA:BB-CC:DD-EE:FF), which is technically non-standard. If strict consistency is needed, you would use two separate patterns or a backreference. This pattern covers the most common MAC address formats used in networking tools, DHCP configurations, and device identification.
Example Test Strings
| Input | Expected |
|---|---|
| 00:1A:2B:3C:4D:5E | Match |
| AA-BB-CC-DD-EE-FF | Match |
| 00:1A:2B:3C:4D | No Match |
| GG:HH:II:JJ:KK:LL | No Match |
| 01:23:45:67:89:ab | Match |
Try It — Interactive Tester
Match Highlighting(3 matches)
Matches & Capture Groups
37 charsFlags: giMatches: 3Ctrl+Shift+C to copy regex
Related Regex Patterns
Regex to Match IPv4 Addresses
/\b(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\b/g
Regex to Match IPv6 Addresses
/(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}/g
Regex to Match Hexadecimal Numbers
/^(?:0x)?[0-9a-fA-F]+$/i
Regex to Match CIDR Notation
/\b(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\/(?:3[0-2]|[12]?\d)\b/g