I'm checking a string with this regular expression:/^[SE]*[\s]*[0-9]{3}[\s\-]*[0-9]{2}$/. This allows a total of 9 different formats, being: XXXXX, XXX-XX, XXX XX, SEXXXXX, SEXXX-XX, SEXXX XX, SE XXXXX, SE XXX-XX and SE XXX XX (X:s being any number from 0-9). How do I change the format to XXXXX after the check passes for one of the 9 valid formats?
I've read a bunch of other threads about the string.replace()-method but I can't seem to make it work. Here's my code so far:
var pcValue = postalCode.value;
var format = /^[SE]*[\s]*[0-9]{3}[\s\-]*[0-9]{2}$/;
if (format.test(pcValue) === true) {
pcValue = pcValue.replace(/\[SE]*[\s]*[\s\-]*$/, /^[0-9]{5}$/);
}
What it gives me is a string with /^[0-9]{5}$/ instead of the XXXXX format I wanted.