首页 文章

我如何检测Imgur图片链接是什么,什么不是?

提问于
浏览
1

我正在尝试以编程方式确定链接是否是指向Imgur图像的链接 . Imgur图像链接的一个示例是:http://imgur.com/0AKSCQ4http://i.imgur.com/0AKSCQ4.jpg(第一个是间接链接,后者是直接链接,但ID保持不变)

当被问及是否有Imgur链接,但是http://imgur.com/galleryfalse 时,我希望http://imgur.com/0AKSCQ4评估为 true . 我'm confused how to distinguish between those two when they'都是 imgur.com/*letters* .

我问,因为我知道Reddit Enhancement Suite有这个功能 . 如果我发布http://imgur.com/gallery它没有提供图像按钮来预览它,但它会为http://imgur.com/0AKSCQ4

那我怎么能识别这个呢?查找不符合条件的每个单词,例如_2837009中的 galleryjobsabout ,看起来真的很骇人听闻,并会破坏任何添加的新页面 . 第二部分并不总是数字,所以我不能依靠它来识别它 .

2 回答

  • 2

    Run this snippet for a JavaScript example

    $(function(){
      
        var url_re = /https?[^<"]+/g  /* pattern for url-like substrings */
        
        var txt = $(".post-text").html(); /* taking this question text as input */
      
    	while(m = url_re.exec(txt)){ /* match all url-like substrings in input */
          
            /* verify if it's a imgur URL */
          
    		var imgur_re = /^https?:\/\/(\w+\.)?imgur.com\/(\w*\d\w*)+(\.[a-zA-Z]{3})?$/
            
            
            /* Show result */
            
            $("#results").append("<li>" + m + ": " + imgur_re.test(m) + "</li>");
    	}
      
    });
    
    <ul id="results"></ul>
    
    <div class="post-text" itemprop="text">
    <p>I'm trying to programmatically figure out whether or not an link is a link to an Imgur image or not. An example of an Imgur image link would be: <a href="http://imgur.com/0AKSCQ4" rel="nofollow">http://imgur.com/0AKSCQ4</a> or <a href="http://i.imgur.com/0AKSCQ4.jpg" rel="nofollow">http://i.imgur.com/0AKSCQ4.jpg</a> (the first is an indirect link and the latter is direct, but the ID stays the same)</p>
    
    <p>I want <a href="http://imgur.com/0AKSCQ4" rel="nofollow">http://imgur.com/0AKSCQ4</a> to evaluate to <code>true</code> when asked if an Imgur link, but <a href="http://imgur.com/gallery" rel="nofollow">http://imgur.com/gallery</a> to be <code>false</code>. I'm confused how to distinguish between those two when they're both <code>imgur.com/*letters*</code>.</p>
    
    <p>I ask because I know <a href="http://redditenhancementsuite.com" rel="nofollow">Reddit Enhancement Suite</a> has this functionality. If I post <a href="http://imgur.com/gallery" rel="nofollow">http://imgur.com/gallery</a> it doesn't offer an image button to preview it, but it would for <a href="http://imgur.com/0AKSCQ4" rel="nofollow">http://imgur.com/0AKSCQ4</a></p>
    
    <p>So how would I be able to identify this? Finding every word that doesn't qualify, like <code>gallery</code>, <code>jobs</code>, or <code>about</code> in <code>imgur.com/*whatever*</code> would seem really hacky, and would break upon any new page being added. And there's not <em>always</em> numbers in the second part so I can't rely on that to identify it.</p>
    </div>
    
    
    <script type="text/javascript" src="//code.jquery.com/jquery-2.1.1.min.js"></script>
    
  • 0

    这个正则表达式:

    /(?>https?:)?\/\/(\w+\.)?imgur\.com\/(\S*)(\.[a-zA-Z]{3})/m
    

    将匹配:

    https://i.imgur.com/55bm5QS.png
    //i.imgur.com/JiAKOkJ.png
    http://i.imgur.com/JiAKOkJ.png
    

    由kums提供的正则表达式与此不匹配:http://i.imgur.com/JiAKOkJ.png

相关问题