views:

58

answers:

3

I've been at this for hours -- I think I need sleep... but no matter how I alter the expression javascript will only capture the 1st and 3rd elements:

var number = 09416;
var mat = "([0-9]+/[0-9]+/[0-9]+\\s+[0-9]+:[0-9]+)\\s+([A-Z]+)\\s+[0-9,]+\\s+(.*?"+number+".+)";
//          month / day / year      hour  :  min        AMPM      byte size    filename containing number in middle
var pattern = new RegExp(mat,"gi");
var arr = ['09/07/2010  07:08 PM                1,465,536 BOL09416 BOL31.exe',
           '09/06/2010  12:13 PM                  110,225 BOL09416_BOL030.exe',
           '09/08/2010  04:46 AM                   60,564 BOL09416_BOL32.exe',
           '09/08/2010  01:08 PM                   63,004 bol09416_bol33.exe']
for (var i=0;i<arr.length;i++){
     var match = pattern.exec(arr[i]);
     alert(match);
}

It is all spaces (no tabs), I've rewriten the regex to be as explainatory as possible... It correctly matches on arr[0] and arr[2], but nulls on the other two. Tried looking for possible typo's, trying different .+,.*,.+? etc. All online matchers show that it should be working: Example

Anybody have any ideas as to what I'm missing?

====================
Update:

Going through all the awesome suggestions I am stumped even further:

var match = arr[i].match(/([0-9]+\/[0-9]+\/[0-9]+\s+[0-9]+:[0-9]+)\s+([A-Z]+)\s+[0-9,]+\s+(.*?09416.+)/g);

gives match[0] = full string match[1] = undefined. Basically no captures.

where as:

var match = /([0-9]+\/[0-9]+\/[0-9]+\s+[0-9]+:[0-9]+)\s+([A-Z]+)\s+[0-9,]+\s+(.*?09416.+)/g.exec(arr[i]);

DOES return match[0] = full string, match[1] = date, and so on.

So I guess my real question is how to include dynamically made RegExpressions, and have multiple captures? As the only difference between:

var number = "09416";
var mat = "([0-9]+/[0-9]+/[0-9]+\\s+[0-9]+:[0-9]+)\\s+([A-Z]+)\\s+[0-9,]+\\s+(.*?09416.+)";
var pattern = new RegExp(mat,'g');

and

/([0-9]+\/[0-9]+\/[0-9]+\s+[0-9]+:[0-9]+)\s+([A-Z]+)\s+[0-9,]+\s+(.*?09416.+)/g.exec(arr[i]);

is that I hard-typed the number.

A: 

Try this regex:

new RegExp('([0-9]+/[0-9]+/[0-9]+\\s+[0-9]+:[0-9]+)\\s+(AM|PM)\\s+([0-9,]+)\\s+([^0-9]*'+number+'.+)','gi')
  1. Time
  2. AM/PM
  3. File size
  4. File name
M28
This gives nulls (no match) on all 4 =( Tried changing string.match instead of regex.exec and same results. Thank you though.
WSkid
I forgot to escape the \, try again please.
M28
Now it catches 1 and 3, still skipping the other two =( maybe I'm just crazy.
WSkid
A: 

I would suggest you try Regexr to build the expression.

try this

([0-9]+/[0-9]+/[0-9]+ +?[0-9]+:[0-9]+) +?([A-Z]+) +?[0-9,]+ +?(.*?09416.*)
cofiem
That's what I originally used, and it shows up as matching in their system, but in my page (IE7 unfortunately) it still captures just the 1 and 3 elements. Thank you for the suggestion though.
WSkid
+1  A: 

Hello mister,

var number = 09416;
//          month / day / year      hour  :  min        AMPM      byte size    filename containing number in middle
var mat = '^(\\d{2}\\/\\d{2}\\/\\d{4})\\s+(\\d{2}:\\d{2}\\s*[AP]M)\\s+((\\d+[\\d,]?)*)\\s+(.*' + number + '.*)$';

var pattern = new RegExp(mat);

var arr = ['09/07/2010  07:08 PM                1,465,536 BOL09416 BOL31.exe',
           '09/06/2010  12:13 PM                  110,225 BOL09416_BOL030.exe',
           '09/08/2010  04:46 AM                   60,564 BOL09416_BOL32.exe',
           '09/08/2010  01:08 PM                   63,004 bol09416_bol33.exe']

for (var i=0;i<arr.length;i++){
     var match = arr[i].match(pattern);
     console.log(match);
}

Use string.match instead of regex.exec.

Edited I've removed the global and it worked like it should be. I've also rewritten the regex but it's quite close to yours (not a big deal).

Look at the output by firebug below:

["09/08/2010 04:46 AM ...,564 BOL09416_BOL32.exe", "09/08/2010", "04:46 AM", "60,564", "564", "BOL09416_BOL32.exe"]

0 "09/08/2010 04:46 AM ...,564 BOL09416_BOL32.exe" //whole match
1 "09/08/2010" //date
2 "04:46 AM" //time
3 "60,564" //bytes
4 "564" // last digit of bytes (i can't take this off. but it's harmless)
5 "BOL09416_BOL32.exe" //name of file
sheeks06
using string.match returns true on all 4, but how do I access the captured groups? I looked around a little bit and most using multiple capture groups on single lines used exec. What I mean to say is, even though we are using the 'g' for global, console.log(match[0]+" "+match[1]); gives an error -- as if its not capturing the groups (where [0] would be the full line).
WSkid
tested it in firebug and it seems fine now.
sheeks06
thank you! working beautifully now minus the global flag - which seems opposite of the various documentation - but whatever works.
WSkid
Your welcome! You can sleep now. :)
sheeks06