1

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.

2
  • exec will probably not group them for you recursively. Are you sure you aren't aiming for console.log('3d 4m 5y'.match(myregexp));? Commented Apr 19, 2011 at 20:39
  • 3
    This doesn't have much to do with your problem, but you should remove the commas from your character class: [dmy] is what you want. Otherwise, the comma would also be matched. Commented Apr 19, 2011 at 20:41

2 Answers 2

2

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);
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.