Stack Overflow archive
1 scoreaccepted

detect all urls in img tag in a large string + android

score
1
question views
342
license
CC BY-SA 3.0

Unless you are Chuck Norris or Jon Skeet, you shouldn't use RegEx to match HTML. I would suggest using Jsoup. Here is an example using the string from your question:

js
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();

Originally posted on Stack Overflow. Public user contributions are licensed under Creative Commons Attribution-ShareAlike.