Formatting Canadian postal codes with .splice()
javascript
Solution
'm5r2g3'.toUpperCase().replace(/\W/g,'').replace(/(...)/,'$1 ') // "M5R 2G3"
The `replace(/\W/g,'')` removes all non-alphanumeric characters (including commas).
Problem
I need to properly format a Canadian zip code if its entered in wrong. Format is ### ### where "#" can be either a number or letter for example : M5R 2G3 I've tried this: (its broken up for testing purposes) ``` shipping.zip = shipping.zip.toUpperCase().split('') shipping.zip = shipping.zip.splice(3, 0, ' ') shipping.zip = shipping.zip.join().replace(/,/g, ''); ``` But when I enter in : m5r2g3 I Get this: [ 'M', '5', 'R', '2', 'G', '3' ] [ ] And thats it. I have no idea why its not working. Please help. Thanks.