0

I have a part of HTML from a website in the below String format:

srcset=" /tesla_theme/assets/img/homepage/mobile/[email protected]?20170808 200w, /tesla_theme/assets/img/homepage/mobile/[email protected]?20170808 338w, /tesla_theme/assets/img/homepage/mobile/[email protected]?20170808 445w, tesla_theme/assets/img/homepage/mobile/[email protected]?20170808 542w, /tesla_theme/assets/img/homepage/mobile/[email protected]?20170808 750w"

I want to add http://tesla.com in front of all the urls in the srcset element like http://tesla_theme/assets/img/homepage/mobile/[email protected]?20170808 750w

I believe this could be done using regex, but I am not sure.

How do I do this using Java if I have multiple srcset elements in a html string variable, and I want to replace all of the srcset url.'s and add the server url in front?

Note: The /tesla_theme will not be consistent, so I cannot use replaceAll, instead, i will have to use regex.

4
  • 2
    You have a complete html in string format in java? or just the srcset? you can try this srcset = srcset.replaceAll("/tesla_theme", "tesla.com/tesla_theme"); Commented Aug 14, 2017 at 18:18
  • I have a complete html in string format. And Also its not specific to just tesla.com, but any also keeping in mind that it could be for other Urls too. Commented Aug 14, 2017 at 18:20
  • Do you mean relative URL to http appended URL? Commented Aug 14, 2017 at 18:29
  • Correct. It is not just specific to tesla. I have a tool which gets html from a url provided. So in this html it gets, if there is a srcset element, I want the links inside the srcset appended with the host url in front. Commented Aug 14, 2017 at 18:48

1 Answer 1

1

You can simply use String Class replace method as below, It will replace all "/_tesla" in the given String. No special regex required unless you have a kind of pattern instead of "/tesla"

String srcset=" /tesla_theme/assets/img/homepage/mobile/[email protected]?20170808 200w, /tesla_theme/assets/img/homepage/mobile/[email protected]?20170808 338w, /tesla_theme/assets/img/homepage/mobile/[email protected]?20170808 445w, tesla_theme/assets/img/homepage/mobile/[email protected]?20170808 542w, /tesla_theme/assets/img/homepage/mobile/[email protected]?20170808 750w";

String requiredSrcSet = srcset.replace("/tesla_", "http://tesla_");
Sign up to request clarification or add additional context in comments.

1 Comment

There is a pattern. It won't be tesla all the time. So since it will not be consistent, I will need to use regex

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.