Regex for MAC address

Use /^(?:[0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}$/ to check that a whole string is a valid for MAC address. Every example below is tested against the pattern, and you can open it in the Regex Tester with the samples preloaded.

Updated

/^(?:[0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}$/
Open in Regex Tester
InputResult
00:1A:2B:3C:4D:5E✓ Match
00-1a-2b-3c-4d-5e✓ Match
001A2B3C4D5E✗ No match
00:1A:2B:3C:4D✗ No match
00:1G:2B:3C:4D:5E✗ No match

How the pattern works

PartMeaning
[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 false

Python

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.