开发者

javascript dynamic regex vs hard typed and captures

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?

====================

U开发者_如何学Pythonpdate:

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.


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


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


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜