Regex for MAC address

/\b[0-9A-Fa-f]{2}(?:([:-])[0-9A-Fa-f]{2})(?:\1[0-9A-Fa-f]{2}){4}\b/g

Options
Matches

Examples

Note

The backreference \1 makes every separator match the first, so mixed colons and dashes are rejected.

Matches in the example

  • "00:1A:2B:3C:4D:5E"
  • "00-1a-2b-3c-4d-5f"

Token by token

  1. \b a word boundary
  2. [0-9A-Fa-f]{2} one of: “0” to “9”, “A” to “F” or “a” to “f”, exactly 2 times
  3. (?: a group, not capturing
  4. ( a capturing group
  5. [:-] one of: “:” or “-”
  6. ) end of group
  7. [0-9A-Fa-f]{2} one of: “0” to “9”, “A” to “F” or “a” to “f”, exactly 2 times
  8. ) end of group
  9. (?: a group, not capturing
  10. \1 the same text group 1 matched
  11. [0-9A-Fa-f]{2} one of: “0” to “9”, “A” to “F” or “a” to “f”, exactly 2 times
  12. ){4} end of group, whole group exactly 4 times
  13. \b a word boundary

Other patterns