簡體   English   中英

Java正則表達式可啟用文本內的鏈接,但已包含的標記除外

[英]Java regex to enable link inside text except already enclosed tag

假設一個用戶輸入的文本包含HTML以及可能的鏈接,我想啟用鏈接,並完整地保存has_tag_closed網址。

(我知道有很多正則表達式網址模式問題,但我找不到此解決方案)

例如 :

String urlRegex = "((https?|ftp|gopher|telnet|file):((//)|(\\\\))+[\\w\\d:#@%/;$()~_?\\+-=\\\\\\.&]*)";
Pattern urlPattern = Pattern.compile(urlRegex, Pattern.CASE_INSENSITIVE);

String s = ...
urlPattern.matcher(s).replaceAll("<a href='$0' target='_blank'>$0</a>")

可以將"google https://google.com"google <a href='https://google.com' target='_blank'>https://google.com</a> ,很好。

但是如果字符串是

"<a href=\"http://www.google.com/\">google</a> " +
" http://www.google.com/  " +
" <a href=\"https://facebook.com/\">facebook</a> " +
" https://facebook.com ";

它將成為

<a href="<a href='http://www.google.com/' target='_blank'>http://www.google.com/</a>">google</a>  <a href='http://www.google.com/' target='_blank'>http://www.google.com/</a>   <a href="<a href='https://facebook.to/' target='_blank'>https://facebook.to/</a>">facebook</a>  <a href='https://facebook.com' target='_blank'>https://facebook.com</a> 

它不應該觸摸href值,因此我將urlRegex更改為:

urlRegexExceptAnchor = "(?<!\\<a\\ href=\")(http|https):\\/\\/[^ ]*"; 

好吧,它可以處理混合了錨標記的文本。

但是 ,如果文本包含iframe ,它將再次失敗:

<iframe src="https://www.facebook.com/plugins/post.php?href=https%3A%2F%2Fwww.facebook.com%2Fnytimes%2Fposts%2F10151112309519999&width=500" width="500" height="525" style="border:none;overflow:hidden" scrolling="no" frameborder="0" allowTransparency="true"></iframe>

<iframe src="<a href='https://www.facebook.com/plugins/post.php?href=https%3A%2F%2Fwww.facebook.com%2Fnytimes%2Fposts%2F10151112309519999&width=500"' target='_blank'>https://www.facebook.com/plugins/post.php?href=https%3A%2F%2Fwww.facebook.com%2Fnytimes%2Fposts%2F10151112309519999&width=500"</a> width="500" height="525" style="border:none;overflow:hidden" scrolling="no" frameborder="0" allowTransparency="true"></iframe>

再次無效。

我認為我將面臨越來越多的情況,因為有很多接受URL的標簽 我無法逃脫aiframe代碼...

文本是由用戶輸入的,請確保我可以過濾掉一些無效的標簽,例如formheadinput ...,但是仍然有很多標簽需要處理...(甚至是內聯的CSS背景url)

我現在可以想到的是使用JSoup東西將整個文本傳輸到html doc,並一個接一個地處理textNode。 但是我認為這太過分了。 (每個頁面顯示都將調用JSoup ...)

有沒有更簡單的方法來實現這一目標?

對於任何面臨類似問題的人,這是我的JSoup解決方案:

  private static void processNode(Node node) {
    if (node instanceof TextNode) {

      Node parent = node.parent();
      if (parent != null && (StringUtils.equalsAnyIgnoreCase(parent.nodeName(),
        "a", "iframe", "embed" , "img" , "object" , "script" , "video" , "applet"))) {
        logger.debug("parent = {} , skipped", parent.nodeName());
      }
      else {
        TextNode textNode = (TextNode) node;

        String text = textNode.text();
        text = urlPattern.matcher(text).replaceAll("<a href='$0' target='_blank'>$0</a>");

        TextNode r = new TextNode(text , null);
        node.replaceWith(r);
      }
    } else if (node instanceof Element) {
      Element ele = (Element) node;
      for (Node childNode : ele.childNodes()) {
        processNode(childNode);
      }
    }
  }

運作良好...(目前)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM