0
        if ((urlStr.indexOf('t='))!=-1)
        {
            var pat = /t=(\d+)m(\d+)s/;
            pat.exec(urlStr);
            alert (RegExp.$1);
            alert (RegExp.$2);

        }

case 1: http://localhost/proc1/commit.php&t=1m13s Returns 1 and 13 -> Okay

case 2: http://localhost/proc1/commit.php&t=13s Returns blank and blank -> Not okay
Expected Result 0 and 13

How do I have to change my regex?

1 Answer 1

1

You could try this:

var pat = /t=(?:(\d+)m)?(\d+)s/;

This allows for the first part, including the m to be optional. Now in your second case, $1 should be an empty string.

The (?: makes sure, that you do not get another captured string containing the m.

This will work, too, and do pretty much the same:

var pat = /t=(\d*?)m?(\d+)s/;

Here we just allow the first string of digits to be empty, and m to be optional. Just make sure to use ? after the * to make the repetition ungreedy - otherwise the 1 will be matched by the first repetition, m will be left out, and the 3 will be matched by the second repetition.

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

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.