JavaScript negative lookbehind issue

后端 未结 3 1491
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-20 19:51

I\'ve got some JavaScript that looks for Amazon ASINs within an Amazon link, for example

http://www.amazon.com/dp         


        
相关标签:
3条回答
  • 2021-01-20 20:43

    You need to use a lookahead to filter the /e/* ones out. Then trim the leading /e/ from each of the matches.

    var source; // the source you're matching against the RegExp
    var matches = source.match(/(?!\/e)..\/[A-Z0-9]{10}/g) || [];
    var ids = matches.map(function (match) {
      return match.substr(3);
    });
    
    0 讨论(0)
  • 2021-01-20 20:46

    In your case an expression like this would work:

    /(?!\/e)..\/([A-Z0-9]{10})/
    
    0 讨论(0)
  • 2021-01-20 20:49

    ([A-Z0-9]{10}) will work equally well on the reverse of its input, so you can

    1. reverse the string,
    2. use positive lookahead,
    3. reverse it back.
    0 讨论(0)
提交回复
热议问题