Java 检测大字符串中img标记中的所有URL+;安卓
我在android中有一个字符串内容,如:Java 检测大字符串中img标记中的所有URL+;安卓,java,android,Java,Android,我在android中有一个字符串内容,如: xyz xyx xyz<br /><img style="max-width: 100%;" src="https://blablab.png" alt="Loading..." /></p>abc</p><img style="max-width: 100%;" src="https://blablab2.png" alt="Loading..." /><div>abc</
xyz xyx xyz<br /><img style="max-width: 100%;" src="https://blablab.png" alt="Loading..." /></p>abc</p><img style="max-width: 100%;" src="https://blablab2.png" alt="Loading..." /><div>abc</div><img style="max-width: 100%;" src="https://blablab3.png" alt="Loading..." />
xyzxyz
abcabc
在这里,我必须检索所有img标记的src属性(图像文件的url)的值,并用它的base 64值替换img标记。如何做到这一点,即首先检测所有src属性的值,然后用它们的基64值替换img标记?您可以使用
匹配器。下页给出了一些示例:。其思想是编写一个正则表达式,将img
-标记与src
-属性相匹配,找到每个匹配项,从正则表达式组中获取非常量部分,然后进行替换。除非您是Chuck Norris或Jon Skeet,否则不应该使用正则表达式来匹配HTML。我建议使用。下面是一个使用问题字符串的示例:
String html = "xyz xyx xyz<br /><img style=\"max-width: 100%;\" src=\"https://blablab.png\" alt=\"Loading...\" /></p>abc</p><img style=\"max-width: 100%;\" src=\"https://blablab2.png\" alt=\"Loading...\" /><div>abc</div><img style=\"max-width: 100%;\" src=\"https://blablab3.png\" alt=\"Loading...\" />";
Document document = Jsoup.parse(html);
Elements imgs = document.select("img[src]");
for (Element img : imgs) {
img.attr("src", "http://placehold.it/350x150");
}
String newHtml = document.html();
String html=“xyz xyz xyz
abcabc”;
documentdocument=Jsoup.parse(html);
元素imgs=document.select(“img[src]”);
对于(元素img:imgs){
img.attr(“src”http://placehold.it/350x150");
}
字符串newHtml=document.html();
我如何用它的基64值替换img标记?谷歌“替换标记jsoup站点:stackoverflow.com”