New blogMaking an AI Agent You Can Trust With Money

★ Dev formats · regex

Regex for MAC Address

Matches 48-bit MAC addresses written as 00:1A:2B:3C:4D:5E, 00-1a-2b-3c-4d-5e or Cisco's 001A.2B3C.4D5E. Each branch uses one separator, so mixed forms are rejected without needing a backreference.

  • Runs in your browser
  • No signup

Try the mac address pattern

Read it in plain English (best-effort)

  • ^start of a line
  • (?:start a group (not captured)
  • (?:start a group (not captured)
  • [0-9A-Fa-f]one character from: 0–9, A–F, a–f
  • {2}exactly 2 of the previous
  • :the character ':'
  • )end of group
  • {5}exactly 5 of the previous
  • [0-9A-Fa-f]one character from: 0–9, A–F, a–f
  • {2}exactly 2 of the previous
  • |or
  • (?:start a group (not captured)
  • [0-9A-Fa-f]one character from: 0–9, A–F, a–f
  • {2}exactly 2 of the previous
  • -the character '-'
  • )end of group
  • {5}exactly 5 of the previous
  • [0-9A-Fa-f]one character from: 0–9, A–F, a–f
  • {2}exactly 2 of the previous
  • |or
  • (?:start a group (not captured)
  • [0-9A-Fa-f]one character from: 0–9, A–F, a–f
  • {4}exactly 4 of the previous
  • .the character '.'
  • )end of group
  • {2}exactly 2 of the previous
  • [0-9A-Fa-f]one character from: 0–9, A–F, a–f
  • {4}exactly 4 of the previous
  • )end of group
  • $end of a line

One sample per line works well with the m flag on anchored patterns.

Waiting for a pattern
01

The pattern, piece by piece

/^(?:(?:[0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}|(?:[0-9A-Fa-f]{2}-){5}[0-9A-Fa-f]{2}|(?:[0-9A-Fa-f]{4}\.){2}[0-9A-Fa-f]{4})$/
Pattern explanation
PartWhat it does
(?:[0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}six hex pairs separated by colons
(?:[0-9A-Fa-f]{2}-){5}[0-9A-Fa-f]{2}or separated by hyphens
(?:[0-9A-Fa-f]{4}\.){2}[0-9A-Fa-f]{4}or Cisco's three dotted groups of four
02

What it accepts and rejects

Every sample below is checked automatically in JavaScript and Python. The tester above is preloaded with all of them, one per line.

Should match
Should match
00:1A:2B:3C:4D:5E✓
00-1a-2b-3c-4d-5e✓
aa:bb:cc:dd:ee:ff✓
001A.2B3C.4D5E✓
FF:FF:FF:FF:FF:FF✓
01:23:45:67:89:ab✓
Should not match
Should not match
00:1A:2B:3C:4D✗
00:1A:2B:3C:4D:5E:6F✗
00:1A-2B:3C:4D:5E✗
001A2B3C4D5E✗
GG:1A:2B:3C:4D:5E✗
00:1A:2B:3C:4D:5✗
03

Copy it into your code

RE2-safeNo lookarounds or backreferences, so the same pattern works in Go's RE2 engine and in Rust's regex crate.

const macAddressRe = /^(?:(?:[0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}|(?:[0-9A-Fa-f]{2}-){5}[0-9A-Fa-f]{2}|(?:[0-9A-Fa-f]{4}\.){2}[0-9A-Fa-f]{4})$/;

export function isValidMacAddress(value) {
  return macAddressRe.test(value.trim());
}
04

When not to use regex for this

To normalise MACs for storage or comparison, strip separators, lowercase, and compare the 12 hex characters.

05

Questions engineers actually ask

Why three branches instead of one with a backreference?
A backreference could enforce a consistent separator, but Go's RE2 doesn't support backreferences. Separate branches work in every engine.
Does it accept 12 hex digits with no separators?
No, to avoid matching random hex strings. Add a fourth branch [0-9A-Fa-f]{12} if you need it.
What is catastrophic backtracking?
Some patterns, usually nested quantifiers like (a+)+, make a backtracking engine try an exponential number of paths on input that almost matches. The tester stops any run after 300ms, so a bad pattern can't freeze your tab. In production, that same pattern can take down a server (a ReDoS attack).
06

More dev formats patterns

07

More from the workbench

Fine print

Tools: on the house.
Engineer: by the hour.

The regex was free. Code that doesn't need one is the upgrade: typed schemas, validated once at the edge, trusted everywhere after.

I'm Himanshu, an AI and full-stack engineer. I build MVPs, AI agents and the growth plumbing around them for early-stage startups.