1

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.

4 Answers 4

1

You can do this :

var format = /^([SE]*)\s*(\d{3})[\s\-]*(\d\d)$/;
if (format.test(pcValue)) {
    pcValue = pcValue.replace(format, "$2$3");
}

Note that if you don't do anything else than replacing if test returns true, then you don't have to test at all, just execute the replacement.

Sign up to request clarification or add additional context in comments.

2 Comments

It works but if there is a SE at the beginning it's still there after the replacement. How do i remove it aswell?
@Lemonmuncher Edited to remove the SE
0

Try this:

var pcValue = postalCode.value;
pcValue = pcValue.replace(/^[SE]*[\s]*([0-9]{3})[\s\-]*([0-9]{2})$/, "$1$2");

Demo http://jsfiddle.net/uLJEm/

1 Comment

Worked like a charm! Even removed the eventual SE's.
0
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}$/);
}

str.replace(original,replicant);

Comments

0

The answer you've marked as accepted also validates these kinds of inputs :

"ES 12345"
"SSSSS 123 -  45"
"SESE     123---45"

The following regular expression might better fit with your needs :

function parse(input) {
    var re = /^(?:SE)?\s?(\d{3})(?:\s|-)?(\d\d)$/;
    return re.test(input) && input.replace(re, '$1$2');
}
var input1 = parse('SE 123 45'); // 12345
var input2 = parse('ES 123 45'); // false
input1 || alert('Please fix input #1!');
input2 || alert('Please fix input #2!'); // alert!

A little help about /^(?:SE)?\s?(\d{3})(?:\s|-)?(\d\d)$/ :

^...$       string boundaries
(...)       capture match
(?:...)     don't capture match

(?:SE)?     "SE", zero or one time
\s?         a whitespace, zero or one time
(\d{3})     3 digits (captured -> $1)
(?:\s|-)?   a whitespace or a dash, zero or one time
(\d\d)      2 digits (captured -> $2)

More on regular expressions : http://www.javascriptkit.com/javatutors/redev.shtml.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.