I have
String s = "<a href="https://stackoverflow.com">https://stackoverflow.com</a><br/><a href="https://google.com">https://google.com</a>"
Now I just want to replace all links in the href
attributes, by prefixing with a fixed value (e.g. `abc.com?'). Here's the result that I want:
String s = "<a href="abc.com?url=https://stackoverflow.com">https://stackoverflow.com</a><br/><a href="abc.com?url=https://google.com">https://google.com</a>"
I tried the following, but it doesn't resolve the problem because it replaces all strings beginning http://
, not only those within href
attributes:
s= s.replaceAll("http://.+?(com|net|org|vn)/{0,1}","abc.com" + "&url=" + "$0");
What can I do to replace only within the attribute, and not in other content?