JavaScript负面的后视问题 [英] JavaScript negative lookbehind issue
问题描述
我有一些JavaScript在Amazon链接中查找Amazon ASIN,例如
I've got some JavaScript that looks for Amazon ASINs within an Amazon link, for example
http://www.amazon.com/dp/B00137QS28
<为此,我使用以下正则表达式: /([A-Z0-9] {10})
但是,我不希望它匹配艺术家链接,如下所示:
However, I don't want it to match artist links which look like:
http://www.amazon.com/Artist-Name/e/B000AQ1JZO
所以我需要排除任何在斜杠前面有一个'/ e'和10个字符的字母数字代码的链接。我认为以下内容会这样做:(?<!/ e)([A-Z0-9] {10})
,但结果却是负面的看法用JavaScript工作。是对的吗?有没有另一种方法可以做到这一点?
So I need to exclude any links where there's a '/e' before the slash and the 10-character alphanumeric code. I thought the following would do that: (?<!/e)([A-Z0-9]{10})
, but it turns out negative lookbehinds don't work in JavaScript. Is that right? Is there another way to do this instead?
非常感谢任何帮助!
作为旁注,请注意有很多亚马逊链接格式,这就是我想黑名单而不是白名单的原因,例如,这些都是同一页:
As a side note, be aware there are plenty of Amazon link formats, which is why I want to blacklist rather than whitelist, eg, these are all the same page:
http://www.amazon.com/gp/product/B00137QS28/
http://www.amazon.com/dp/B00137QS28
http://www.amazon.com/exec/obidos/ASIN/B00137QS28/
http://www.amazon.com/Product-Title-Goes-Here/dp/B00137QS28/
推荐答案
在你的情况下,这样的表达式可以起作用:
In your case an expression like this would work:
/(?!\/e)..\/([A-Z0-9]{10})/
这篇关于JavaScript负面的后视问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!