简体   繁体   中英

Javascript regex with lookahead and lookbehind being wrong?

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM