0

First, thanks in advance for any help you can provide.

My regex is only capturing the first 3 instances of the string I'm trying to extract. Any ideas?

My code:

var objArgs         = WScript.Arguments;
var objFileSys      = new ActiveXObject("Scripting.FileSystemObject");
var objFolder       = objFileSys.getFolder(objArgs.Item(0));
var objTSetNum      = new RegExp("^ST.((\\d+)+)", "gm");
var objFile         = null;
var srcFile         = null;

for (var objFileEnum = new Enumerator(objFolder.files); !objFileEnum.atEnd(); objFileEnum.moveNext()) {
objFile = objFileSys.OpenTextFile(objFileEnum.item(), 1, false, 0);
srcFile = objFile.ReadAll();
objFile.Close();

// Check for different values in the TSet entries
// Build array to hold all TSet values found
var arrTSet;
while ((arrTSet = objTSetNum.exec(srcFile)) !== null) {
    // arrTSet = objTSetNum.exec(srcFile);
    WScript.Echo("arrTSet is: " + arrTSet);
    WScript.Echo("Length of arrTSet is: " + arrTSet.length)
    //var allTSetSame = true;
}

And a sample text file that has 5 instances of a line beginning with "ST*" - shouldn't all 5 be placed into the array?:

ISA*00*          *00*          *01*041199639      *08*9272590000     *150704*1131*U*00401*000001324*0*P*:
ST*865*0001
ST*865*0001
ST*865*0001
ST*865*0001
ST*865*0001
IEA*5*000001324

Any advice? My hunch is telling me that the exec() function is changing something, but I can't find any details as to what it might be changing.

Appreciate the help!

EDIT: Of course, once I posted it, I found the answer. exec() is stateful, so I needed to push each result returned into the array, similar to:

var arrTSet = [];
var match;  

  while (match = objTSetNum.exec(srcFile)) {
    arrTSet.push(+match[1]);            
}

Which was found here (giving credit): Why does Javascript's regex.exec() not always return the same value?

Community
  • 1
  • 1
  • Why are you using `^ST.((\\d+)+)` when `^ST.(\\d+)` gives the same result? – d0nut Jul 14 '15 at 16:36
  • Can you use an alert or something to make sure that right before the regular expression is evaluted that the string that's being passed to it is exactly as you expect it to be? That nothing was incorrectly read in? – d0nut Jul 14 '15 at 16:41
  • A remnant of troubleshooting. As you can tell, I was, at that point, throwing sh*t at the wall and seeing if anything sticks. Sorry for not cleaning it up before posting. – SO-Darkater Jul 14 '15 at 16:41
  • awwww crap it seems you figured it out just as I posted it. – d0nut Jul 14 '15 at 16:47
  • No worries - I got you. :) Greatly appreciate the assistance! – SO-Darkater Jul 14 '15 at 16:59

2 Answers2

1

What I think the problem might be is that RegExp.exec doesn't return an array of all possible results, it returns the first result with all the matches in that result. If you look through that array that you get back, your matches will be:

  1. ST*865
  2. 865
  3. 865

Exec will continue to return the next match each time you call it

d0nut
  • 2,835
  • 1
  • 18
  • 23
1

Was already answered here:

Why does Javascript's regex.exec() not always return the same value?

Sorry for the already-answered question.

Community
  • 1
  • 1
  • I don't think that this is *exactly* the same thing. The answer might be the same but the question that brought you here is definitely different. – d0nut Jul 14 '15 at 16:48