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?

asked Oct 19, 2012 at 7:57

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.

answered Oct 19, 2012 at 8:01

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.