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
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.