How can I format a string using Javascript to match a regex?
I am using UK postcodes which could match any of the following
N1 3LDEC1A 3ADGU34 8RR
I have the following regex which validates a string correctly, but I am unsure how to use the regex as a mask to format EC1A3AD
to EC1A 3AD
/ GU348RR
to GU34 8RR
/ N13LD
to N1 3LD
.
My regex is /^[A-Za-z]{1,2}[0-9A-Za-z]{1,2}[ ]?[0-9]{0,1}[A-Za-z]{2}$/
Thank you