Query guide: JavaScript MAC address regex
JavaScript regex MAC address validator for regex.test checks
Use this page when a JavaScript expression needs to accept or reject MAC address strings before a device form, inventory import, network allowlist, or admin search continues. Test colon, hyphen, dotted, compact, and malformed samples online before copying the expression into regex.test().
Searches for javascript regex mac address validator, mac address regex javascript, and check regex javascript usually point to a format-boundary problem. The code needs to know whether a field is exactly an accepted MAC address, not whether a valid-looking address appears somewhere inside a longer note or log line.
The key decision is separator policy. Colon and hyphen formats are common, Cisco dotted notation appears in network tooling, and compact 12-hex values are convenient for storage. Treat those as separate requirements so the validator stays readable and reviewable.
This workflow is JavaScript-first. It validates text shape with regex, then leaves canonical formatting, inventory lookup, vendor metadata, allowlist matching, and network authorization to application code after the string has passed the first gate.
How to validate a MAC address regex
- Decide which MAC address formats the field accepts: colon separated, hyphen separated, Cisco dotted, compact 12-hex, or only one canonical style.
- Use `^` and `$` when `regex.test(value)` must approve the whole MAC address instead of finding one address-shaped fragment in pasted text.
- Require hex pairs with `[0-9A-Fa-f]{2}` so invalid characters such as `G`, `Z`, and whitespace cannot pass.
- Use a separator backreference when colon and hyphen formats are both accepted but mixed separators should fail.
- After regex passes, normalize casing and separators in application code before lookup, storage, allowlists, or device inventory matching.
MAC address expressions to test
Colon or hyphen, consistent separator
^[0-9A-Fa-f]{2}([:-])(?:[0-9A-Fa-f]{2}\1){4}[0-9A-Fa-f]{2}$
Pass: 00:1A:2B:3C:4D:5E, 00-1A-2B-3C-4D-5E
Fail: 00:1A-2B:3C-4D:5E, mac=00:1A:2B:3C:4D:5E
Good for JavaScript form validation when either common separator is allowed but one value cannot mix styles.
Colon-separated MAC address
^(?:[0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}$
Pass: 00:1A:2B:3C:4D:5E, a4:b1:c2:d3:e4:f5
Fail: 00-1A-2B-3C-4D-5E, 00:1A:2B:3C:4D
Use this when UI copy, API docs, or storage rules require only the colon-separated format.
Hyphen-separated MAC address
^(?:[0-9A-Fa-f]{2}-){5}[0-9A-Fa-f]{2}$
Pass: 00-1A-2B-3C-4D-5E, a4-b1-c2-d3-e4-f5
Fail: 00:1A:2B:3C:4D:5E, 00-1A-2B-3C-4D-5G
Useful for exports or device systems that document MAC addresses with hyphen separators.
Cisco dotted MAC address
^(?:[0-9A-Fa-f]{4}\.){2}[0-9A-Fa-f]{4}$
Pass: 001A.2B3C.4D5E, a4b1.c2d3.e4f5
Fail: 00:1A:2B:3C:4D:5E, 001A.2B3C.4D5
Keep this separate from pair-separated validators so network-device imports are easy to audit.
Compact 12-hex MAC address
^[0-9A-Fa-f]{12}$
Pass: 001A2B3C4D5E, a4b1c2d3e4f5
Fail: 00:1A:2B:3C:4D:5E, 001A2B3C4D5G
Accept compact values only when the app explicitly normalizes them before display, comparison, or storage.
Pass and fail samples for MAC address validators
Should pass pair-separated values
00:1A:2B:3C:4D:5E, 00-1A-2B-3C-4D-5E, a4:b1:c2:d3:e4:f5
Should fail malformed values
00:1A:2B:3C:4D, 00:1A:2B:3C:4D:5G, 00:1A-2B:3C-4D:5E
Format decision samples
001A.2B3C.4D5E, 001A2B3C4D5E, 00:1A:2B:3C:4D:5E
Boundary decision samples
mac=00:1A:2B:3C:4D:5E, 00:1A:2B:3C:4D:5E.
Copy-ready JavaScript regex.test examples
Boolean MAC regex.test check
const macPattern = /^[0-9A-Fa-f]{2}([:-])(?:[0-9A-Fa-f]{2}\1){4}[0-9A-Fa-f]{2}$/;
const isMacAddress = macPattern.test(value);Use a non-global anchored expression when each field should return one stable true or false result.
Normalize after validation
const macPattern = /^[0-9A-Fa-f]{2}([:-])(?:[0-9A-Fa-f]{2}\1){4}[0-9A-Fa-f]{2}$/;
const canonicalMac = macPattern.test(value) ? value.replace(/-/g, ':').toUpperCase() : null;Regex decides shape. Casing, separator style, and inventory keys should be explicit normalization steps.
Validate imported device rows
const macPattern = /^[0-9A-Fa-f]{2}([:-])(?:[0-9A-Fa-f]{2}\1){4}[0-9A-Fa-f]{2}$/;
const invalidRows = rows.filter((row) => !macPattern.test(row.mac.trim()));Trim only if the import contract allows surrounding whitespace; otherwise include whitespace in failing fixtures.
Avoid loose partial matches
const looseMac = /[0-9A-Fa-f:.-]{12,17}/;
looseMac.test('mac=00:1A:2B:3C:4D:5E'); // true, but the whole field is not only a MAC addressLoose extraction patterns can help in logs, but they are too permissive for a device-address validator.
MAC address regex decisions to make first
- Should the field accept both uppercase and lowercase hex, or should input normalize before validation?
- Can users paste colon, hyphen, dotted, and compact formats, or does product copy promise only one format?
- Should mixed separators fail even when every pair is valid hex?
- Will frontend, API, import, and inventory-search code share the same accepted format contract?
- Can surrounding text appear in the value, or must the whole field be only the MAC address?
- Do tests include short values, long values, invalid hex characters, mixed separators, and valid-looking embedded fragments?
Test a MAC address regex now
Open the live tester with a separator-consistent MAC address validator and mixed samples already loaded. The failing rows expose short values, invalid hex, mixed separators, dotted notation, compact notation, and partial-match problems.
Test MAC Address RegexRelated Pages on Regex Tester
Open the live MAC regex tester
Load a JavaScript MAC address pattern, flags, and pass/fail samples in the browser tester.
JavaScript regex validator
Build anchored full-field validators with pass samples, fail samples, and copy-ready checks.
JavaScript regex.test validator
Review why boolean field checks should avoid global and sticky RegExp state.
JavaScript regex flags tester
Confirm which flags affect multiline fixture testing, casing, and repeated matches.
JavaScript UUID regex validator
Compare device-address checks with another anchored identifier-validation workflow.
JavaScript regex.test generator
Turn validation rules into maintainable JavaScript expressions and sample banks.
JavaScript regex examples
Review copy-ready JavaScript RegExp examples after checking your validator online.
Regex cheat sheet
Reference anchors, backreferences, character classes, groups, and quantifiers.
JavaScript MAC Address Regex FAQ
What is a JavaScript regex MAC address validator?
It is an anchored regular expression used with the JavaScript RegExp engine to decide whether a string matches an accepted MAC address format, such as colon-separated, hyphen-separated, dotted, or compact hex.
Should a MAC address regex allow both colon and hyphen separators?
Only if the product accepts both formats. When both are allowed, use a separator backreference so mixed values like `00:1A-2B:3C-4D:5E` fail.
Can regex prove a MAC address belongs to a real device?
No. Regex can validate text shape, but ownership, device existence, vendor lookup, network access, and allowlist status must be handled by application logic or infrastructure systems.
Should MAC address validation use the global flag with regex.test?
No for normal field validation. A global regex changes `lastIndex` between calls, which can make repeated `.test()` checks unreliable.