← Back to challenges

Regex Series: Canadian Postal Codes

JavaScriptHardstringsregex

Instructions

Write a regular expression that matches any valid Canadian postal code with any of the following formats:

A1A 1A1
A1A-1A1
A1A1B1

Make the match case insensitive.

Examples

"M3M 0A9" ➞ true

"m4c-1t1" ➞ true

"m45 1t1" ➞ false

"M4C-1T1Z" ➞ false

Notes

This challenge is designed to use Regex only.

javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.
Next: Mini Sudoku