[英]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的标签 。 我无法逃脱a
或iframe
代码...
文本是由用户输入的,请确保我可以过滤掉一些无效的标签,例如form
, head
, input
...,但是仍然有很多标签需要处理...(甚至是内联的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.