0

I'm using the following regex to match the word 'stores' between '/' and '?' with a possible forward slash '/' before the '?' but for some reason it fails saying there's an invalid quantifier. Any idea why it might be wrong and quatifier is that? I tried removing '/?' but it still says the same thing.

var n=str.match(/(?<=\/)stores\/?(?=\?)/);

Thanks!

GGets
  • 416
  • 6
  • 19

1 Answers1

1

I think this is the invalid part: (?<=/) - javascript's lookahead is (?=y); it doesn't support lookbehinds, which is what I'm assuming you were trying to use. This regex should work though:

\/stores\/?\?

which matches:

a forward slash,

followed by the string 'stores',

followed by zero or one forward slash,

followed by a question mark.

xdl
  • 1,080
  • 2
  • 14
  • 22
  • thing is i want to match the word stores only (even without the potentially present '**/**' which i couldn't manage to do anyway) Sorry i am just a newbie to regex. Any tips? http://regexr.com?35omo – GGets Jul 29 '13 at 20:37
  • Oh I see... hmm would something like \/stores(?=\/?\?) work? The lookahead makes sure that the trailing / and ? aren't matched, so now you're left with an an array with a bunch of '/stores' strings in them. Depending on what you want to do with the matches, I guess you can then use array.forEach(function(element, index, array) {array[index] = element.slice(1)} to get rid the /. – xdl Jul 29 '13 at 21:24
  • i see, i was trying to avoid additional processing, but apparently i have no choice thank you very much! – GGets Jul 30 '13 at 08:36