Reputation: 2106
I have a pretty straightforward snippet of JS/jQuery I'm using to extract some content from a page:
(function (idx) {
var product = $(".product_wrapper img")[idx];
return (product && product.src) ? product.src.match(/([^]+)\\_/)[1] : ""
}(0)).split('_')[1]
Works just fine in most browsers, but IE7 & IE8 are throwing errors. In IE7 the error message reads [object Error]
and in IE8 it reads SyntaxError: Expected '/'
.
Anybody know what's wrong with this code? Am I using some syntactic sugar that MSIE doesn't like? Been puzzling over this one and I'm stumped.
Upvotes: 0
Views: 735
Reputation: 32608
The simplest example that breaks (at least in IE8) is this:
"foo".match(/[^]/);
The problem is that ^
in brackets means "negation" so it expects something to negate. IE breaks because of this and it can be solved by escaping the carat (if that's what you're searching for):
"foo".match(/[\^]/);
Firefox seems to interpret [^]
as .
so it doesn't break. It's unclear from your code sample what you are trying to extract from the URLs. If you are looking for carats in the code, escape what you had in your original post. If you are just looking for any character, as it works in firefox, use the .
instead.
Upvotes: 0
Reputation: 730
The first step is to figure out why the "better" browser, IE 8, gives its error. The error it gives appears to refer to a missing regular expression initializer terminator, which is the '/' character (forward-slash). Your syntax for the regular expression works on IE 9:
"foo".match(/([^]+)\\_/);
And I am pretty sure that it will work on IE 8 as well. I recommend verifying if the above works on IE 8, and then checking to make sure that when you see the error, the code actually being run is what you have. To verify that, you can clear the browser cache in IE 8 manually and then reload your code/webpage.
Upvotes: 0
Reputation: 3939
if product.src.match(/([^]+)\\_/)
is null
product.src.match(/([^]+)\\_/)[1]
will cause object Error
Upvotes: 1