jigglyT101
jigglyT101

Reputation: 984

Regular Expression Fails

Anyone help? When I run this I get " invalid quantifier ?<=href= "

var aHrefMatch = new RegExp("(?<=href\=")[^]+?(?=")"); 
var matchedLink = mystring.match(aHrefMatch);

But I know the regular expression is valid.

Any ideas?

Upvotes: 1

Views: 152

Answers (4)

Rithiur
Rithiur

Reputation: 844

Javascript does not support lookbehind assertions. It only supports lookahead ones. The error is produced because it assumes the ? is a quantifier for 0 or 1, but there is no element to quantify at the beginning of a subpattern (started by that ( opening parenthesis)

Also, your string seems to be missing a few backslashes, as the double quotes are not escaped there. It should produce a syntax error.

Perhaps this code could help you do what you are trying to achieve:

var match = mystring.match(/href=\"([^\"]*)\"/);
var matchedLink = match[1];

Upvotes: 9

fredrik
fredrik

Reputation: 17617

Don't really know what you want to do. But if you want to get the link.

var aHrefMatch = new RegExp(/(href\=\")([\w\-\/]+)?(\")/); 
var matchedLink = mystring.match(aHrefMatch)[2];

Upvotes: 0

Jasmeet
Jasmeet

Reputation: 2352

Did you mean to escape the quote after the = sign and after the look ahead ?=. Also if you are just trying to match the href="some text" , then you really don't need look behind and look ahead constructs. The following should do just fine

href=\"[^"]+\"

If you are trying to match something else, please elaborate. Thanks

Upvotes: 1

Steve Harrison
Steve Harrison

Reputation: 125470

You need to escape the double quotes in the regular expression with the standard backslash:

var aHrefMatch = new RegExp("(?<=href\=\")[^]+?(?=\")");

...or you could just use single quotes to specify the string:

var aHrefMatch = new RegExp('(?<=href\=")[^]+?(?=")');

Upvotes: 1

Related Questions