【发布时间】:2015-03-23 17:25:15
【问题描述】:
我正在解析 json 提要并在 android webview 中显示其内容。一切都很好。但现在我想隐藏那个 android webview 中的所有 img 标签。
问题是 webview 中的内容是动态显示的,这意味着我不知道 img 标签参数。所以我需要一些东西来替换以
开头的字符串中的所有内容 <img ... >
以
结尾 </img>
我该怎么做?
【问题讨论】:
我正在解析 json 提要并在 android webview 中显示其内容。一切都很好。但现在我想隐藏那个 android webview 中的所有 img 标签。
问题是 webview 中的内容是动态显示的,这意味着我不知道 img 标签参数。所以我需要一些东西来替换以
开头的字符串中的所有内容 <img ... >
以
结尾 </img>
我该怎么做?
【问题讨论】:
因为这些答案都不适合我,所以我最终决定使用jsoup 过滤和删除所有 img 标签。
使用 jsop 有点复杂,但是很有效!!
编辑 这是一个示例java代码
String content = "<h1>title</h1><img src="http://..."></img>";
if(content != null) {
Document document = Jsoup.parse(content);
document.select("img").remove();
content = document.toString();
}
【讨论】:
如果您自己处理字符串并将其设置为 webview,那么假设您的内容位于名为 oldWebViewContent 的字符串中,请尝试以下操作:
String webViewContentExcludeImage = oldWebViewContent.replaceAll("<img .*?</img>","");
【讨论】:
\n:oldWebViewContent=oldWebViewContent.replaceAll("\\n","");,然后使用答案中的代码。
<img src=""/> 之类的东西,这在本示例中不起作用。事实上,img 标签甚至可能根本没有/。由于 HTML 的性质,正则表达式通常不是最好的使用方法。
/) 标记怎么办? (在W3C site 上使用)