JavaScript regex - positive lookahead -- giving me syntax errors
This piece of regex (?<=hr开发者_JAVA技巧ef\=")[^]+?(?=#_)
is supposed to match everything in a href value except the the hash value and what follows it within the href url.
It appears to work fine under Regex debuggers/testers such as http://gskinner.com/RegExr/
but in javascript it appears to produce syntax error. If i remove the <
from the (?<=)
it works however, that's not the positive lookahead I am looking for.
I am pulling my hair off, as usual, thanks to Regex lol
Please help
(?<=...)
and (?<!...)
are lookbehinds, not lookaheads. Lookbehinds are not supported by Javascript's regular expression engine.
Resources:
- http://www.regular-expressions.info/lookaround.html#lookbehind
- http://www.regular-expressions.info/javascript.html
Lookbehinds are not support as already mentioned.
I don't know which function you use for your regex, but if you use match()
, just add a capture group:
href="(.+?)(?=#)
Which gives you, e.g.:
var str = '<a href="foo#bar"></a>';
var matches = str.match(/href="(.+?)(?=#)/);
// matches[0] = href="foo
// matches[1] = foo // <-- this is what you want
Additional information:
[^]
means, match all characters that are not in this character class. But there are no characters in the class. So it matches any character which is exactly what the dot .
is doing.
The reason your regex works in RegExr is because RegExr is a Flash application written ActionScript, not JavaScript. Although AS advertises itself as being compliant with the EcmaScript standard the same as JS, it's actually much more powerful. Its regex engine is powered by the PCRE library, so it has the same capabilities as PHP.
To get a more accurate picture of what JavaScript regexes can do, use a tester that's actually powered by JavaScript, like this one.
精彩评论