How the pattern works
| Part | Meaning |
|---|---|
[0-9A-Fa-f]{2} | Two hex digits |
[:-] | A colon or dash separator |
(?:…){5} | Five pairs with separators |
[0-9A-Fa-f]{2}$ | The sixth pair |
Code examples
JavaScript
const re = /^(?:[0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}$/;
re.test(value); // true or falsePython
import re
bool(re.fullmatch(r'(?:[0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}', value))PHP
preg_match('/^(?:[0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}$/', $value) === 1;
Notes and limitations
This pattern allows mixed separators (00:1A-2B…). Capture the first separator and reuse it with a backreference — ^[0-9A-Fa-f]{2}([:-])(?:[0-9A-Fa-f]{2}\1){4}[0-9A-Fa-f]{2}$ — to require consistency.
Regex TesterType a pattern and watch matches highlight live in your test text.
Open the tool Frequently asked questions
What is the regex for MAC address?
/^(?:[0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}$/ — it matches the whole string. See the table above for what each part does.
Which examples match?
Of the examples tested, these match: 00:1A:2B:3C:4D:5E, 00-1a-2b-3c-4d-5e. The others don’t.
How do I test this regex?
Click “Open in Regex Tester” — the pattern, flags and sample text load automatically, and matches highlight as you edit.