Janis Peisenieks
Janis Peisenieks

Reputation: 4988

Javascript regexp not working as expected

I've been working on a regexp, that would parse a date from the format of

3d 4m 5y

to an array, so that I could do some manipulations with it.

I have written a regexp like this:

((\d+)([d,m,y]))

What this returns is

["3d", "3d", "3", "d"]

When I believe it should be returning

["3d", "3d", "3", "d","4m","4","m"]

for the string

3d4m

It is implemented in my code like this:

c=console;
myregexp=/((\d+)([d,m,y]))/g;
//myregexp = new RegExp(regexstring);
c.log(myregexp.exec($("#dateInterval").val()));

right now I'm only logging the data, but I do think, that something is wrong here.

Upvotes: 1

Views: 690

Answers (2)

Cheeso
Cheeso

Reputation: 192457

You wrote:

I believe it should be returning

["3d", "3d", "3", "d","4m","4","m"]  

That's not right.

Calling exec() using a regexp that uses the 'g' option tells it to keep processing matches until it is done. The return array is not a set of all the matches. It is the set of all the captures, for the final match attempt. On the first iteration, it gets ["3d", "3d", "3", "d"]. On the 2nd iteration, it gets ["4m", "4m", "4", "m"]. The capture groups from the 1st iteration get replaced. In other words the '3d' that is in the 1st capture in the 1st iteration gets over-written by the 4 from the 1st capture group in the 2nd iteration, and so on.

To grab all the matches, you can walk the string. like this:

function test2()
{
    var value = "3d4m"; // $('#element').val()
    var re="(\\d+)([dmy])";
    var myregexp = new RegExp(re);
    while (value != "")
    {
        say("input: " + value);
        var result = myregexp.exec(value);
        if (result !== null) {
            say("r[1]: " + result[0]);  // 3d on 1st iteration, 4m on 2nd, etc.
            value = value.substr(result[0].length);
        }
    }
}

Upvotes: 2

Demian Brecht
Demian Brecht

Reputation: 21368

I could be off base here, but according to w3 schools:

exec()

This method returns the matched text if it finds a match, otherwise it returns null.

match()

This method returns an array of matches, or null if no match is found.

This would lead me to believe that exec() will only return a single result.

Here's a fiddle using the two different methods with the same regex statement, yielding different results.

Upvotes: 1

Related Questions