/^/
MediumNetworking4 min read

MAC Address Regex

Validates a 48-bit MAC address written as six hexadecimal byte pairs separated by colons or hyphens, such as 00:1A:2B:3C:4D:5E.

#networking#hardware#hex#validation#mac-address

Regex Pattern

^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$

Pattern Breakdown

Hover over a token to see what it does.

^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$
TokenMeaning
^Anchors the match to the very start of the string.
[0-9A-Fa-f]{2}Exactly two hexadecimal digits, forming one byte of the address.
[:-]A single separator character: either a colon or a hyphen.
{5}Repeats the preceding hex-pair-plus-separator group exactly five times.
([0-9A-Fa-f]{2})Captures the sixth and final hex pair, which has no trailing separator.
$Anchors the match to the very end of the string.

Detailed Explanation

What it does

This pattern checks that a string is a well-formed 48-bit MAC (Media Access Control) address made of six two-digit hexadecimal octets. It accepts either colon-separated (00:1A:2B:3C:4D:5E) or hyphen-separated (00-1A-2B-3C-4D-5E) notation, which are the two conventions used by most operating systems and network tools.

Why it works

The core building block `[0-9A-Fa-f]{2}` matches exactly one byte expressed as two hex digits. Wrapping it with a separator and repeating that group five times via `{5}` accounts for the first five octets, and a final standalone hex-pair group closes out the sixth octet without a trailing separator. Anchoring with `^` and `$` guarantees the entire string, not just part of it, conforms to the shape.

Common use cases

  • Validating MAC addresses entered in network configuration forms
  • Filtering or parsing DHCP lease logs and ARP tables
  • Sanitizing device identifiers before storing them in a database
  • Client-side validation for router or IoT device admin panels

Edge cases

  • Mixed separators like 00:1A-2B:3C-4D:5E will still match because each group's separator is checked independently
  • Uppercase, lowercase, and mixed-case hex digits are all accepted (00:1a:2B:3c:4D:5e)
  • A MAC address with dot notation (0011.2233.4455), common on Cisco devices, will not match
  • Leading or trailing whitespace causes the match to fail because of the strict anchors

Limitations

  • Does not enforce a single consistent separator across all groups
  • Does not recognize Cisco-style dot-grouped notation (aaaa.bbbb.cccc)
  • Does not validate whether the address is a real, assigned, or locally-administered MAC (e.g. multicast bit checks)

Interactive Tester

Edit the pattern or text below — matching runs live in your browser.

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

Test Cases

Editable — add your own inputs to see if they pass.

InputExpectedResult
Pass
Pass
Pass
Pass
Pass
Pass
Pass
Pass

Language Variants

Production-ready examples in 12 languages.

const macPattern = /^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$/;

function isValidMac(address) {
  return macPattern.test(address);
}

console.log(isValidMac("00:1A:2B:3C:4D:5E")); // true

Common Mistakes

Forgetting that colons and hyphens can be mixed because each separator is matched independently per group.

Fix: If you need a single consistent separator, capture the first separator and use a backreference, e.g. `^([0-9A-Fa-f]{2})([:-])(?:[0-9A-Fa-f]{2}\2){4}[0-9A-Fa-f]{2}$`.

Assuming this pattern also matches Cisco's dot-grouped notation like 0011.2233.4455.

Fix: Add a separate alternative branch for the dotted format if you need to support Cisco-style addresses.

Not anchoring the pattern and accidentally matching a MAC address embedded inside a longer string.

Fix: Keep the `^` and `$` anchors, or use `\b` word boundaries if you intentionally want to search within larger text.

Performance Notes

  • The pattern has no unbounded quantifiers or nested repetition, so it runs in linear time with no catastrophic backtracking risk.
  • The fixed `{5}` repetition count keeps the engine's work bounded regardless of input length beyond the expected size.
  • Anchoring with `^` and `$` lets the engine fail fast on inputs of the wrong length instead of scanning further.

Browser Compatibility

EngineSupportedNotes
ChromeYes
FirefoxYes
SafariYes
EdgeYes
Node.jsYes