0

I'm doing this in jsfl, which uses the javascript syntax.

I want to check for mp3 or wav files which are not the first in a sequence. A loop feeds a variable 'audioFile' the following values (---a.mp3, -----b.mp3, ----c.mp3). I then check the variable using this code:

var patt=/([b-z])((\.mp3)|(\.wav))/gi;
if(patt.test(audioFile)){}

This returns true for -----b.mp3 but false for ---a.mp3 and ----c.mp3. Shouldn't the 'b' and 'c' files return true?

If I change it to [a-z] it returns true for ---a.mp3 and ----c.mp3 but it returns false for -----b.mp3. Shouldn't all three sound file names return true?

shawn caza
  • 342
  • 2
  • 13
  • I'm assuming that the ----c.mp3 file has an uppercase letter? – Alex W Aug 08 '12 at 21:57
  • So If I understand correctly you have a comma separated list of song names with extension, and you want to check all of them for a valid extension except the first one? – elclanrs Aug 08 '12 at 22:04
  • @AlexW no c.mp3 is lower case. the 'c' even returns true if I change the regex to [a-z]. As I used the 'i' flag I figured it should not be case sensitive. – shawn caza Aug 08 '12 at 22:10
  • @elclanrs I'm checking each file to see if it's a continuation in a sequence. Files are labelled with a,b,c,etc. at the end if they are part of a sequence. – shawn caza Aug 08 '12 at 22:13
  • Still don't quite understand how regex is useful for this. Why do you have so many capture groups if you're not using any of them? I mean you could write this and should yield same results `/[b-z](\.mp3|\.wav)/` – elclanrs Aug 08 '12 at 22:17
  • Here's a fiddle for people to play with: http://jsfiddle.net/nnnnnn/tWktV/ - I get the same results (false, true, false). – nnnnnn Aug 08 '12 at 22:18
  • If you remove `g` flag then you get `true true true` as expected with `[a-z]` or `false true true` with `[b-z]`. – elclanrs Aug 08 '12 at 22:20
  • Here's a weird one: http://jsfiddle.net/nnnnnn/tWktV/1/ - I test the same string twice in a row and get `true` then `false`. If I test it four times in a row I get `true false true false`. But it works without the `g` flag. Any idea why @elclanrs? – nnnnnn Aug 08 '12 at 22:22
  • Probably because of [this](http://stackoverflow.com/questions/1520800/why-regexp-with-global-flag-in-javascript-give-wrong-results). – elclanrs Aug 08 '12 at 22:26
  • @elclanrs - Ha, yes, after posting that last comment I went and actually read the documentation, and then posted an answer with that information. – nnnnnn Aug 08 '12 at 22:27
  • @elclanrs Thanks! I don't need the 'g' anyway in this case, but like nnnnnn I don't really get why the 'g' screws it up. Edit: got it. – shawn caza Aug 08 '12 at 22:27

1 Answers1

1

The problem is that you are using the same regex object to test multiple times with the global g flag set.

From the MDN .test() doco:

"As with exec (or in combination with it), test called multiple times on the same global regular expression instance will advance past the previous match."

In this case I don't think you need the g flag, because you are matching up to the file extension expected at the end of the string - you may even want to do that explicitly with $:

/([b-z])((\.mp3)|(\.wav))$/i
nnnnnn
  • 147,572
  • 30
  • 200
  • 241