0

I have a situation here, I have a String and I must to replace this <br> to <br />. To do it I can use replace or replace all, but some parts of the text I have <br style='font-size: 14px;'> <a><a/> and I need replace to <br style='font-size: 14px;' /> <a><a/> and any others similar situations in the same string;

IN

"<br> text here <br/> text here <br> text here <br style='font-size: 14px;'> <a><a/>"

EXPECTED OUT

"<br /> text here <br /> text here <br /> text here <br style='font-size: 14px;' /> <a><a/>"

Can you help me with this simple logic? replace only <br cases

3
  • 3
    Perhaps the replaceAll method might be of help? Commented Dec 29, 2018 at 14:56
  • you need to use regexp to perform replace of your second example if you do not want to write an HTML paresre Commented Dec 29, 2018 at 15:00
  • Please bear in mind that using regexps for html may be dangerous as explained here: stackoverflow.com/questions/1732348/… Commented Dec 30, 2018 at 11:51

1 Answer 1

1

Maybe this will help. This is a regex code possibility:

package com.jesperancinha.string;

public class StringReplaceBr {

    public static  String closeBrTags(String a){
        return a.replaceAll("<br(\\/)?([a-zA-z0-9='-:; \"]*)>", "<br$2 />");
    }
}

And this is the unit test to check it up:

package com.jesperancinha.string;


import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;

class StringReplaceBrTest {


    @Test
    void closeSingleAlreadyClosed() {
        assertThat(StringReplaceBr.closeBrTags("<br/>"))
                .isEqualTo("<br />");
    }

    @Test
    void closeSingleNotClosed() {
        assertThat(StringReplaceBr.closeBrTags("<br>"))
                .isEqualTo("<br />");
    }

    @Test
    void closeSingleMixedNotClosed() {
        assertThat(StringReplaceBr.closeBrTags("<br style=\"\" somethingElse=''>"))
                .isEqualTo("<br style=\"\" somethingElse='' />");
    }

    @Test
    void closeBrTags() {
        assertThat(StringReplaceBr.closeBrTags("<br> text here <br/> text here <br> text here <br style='font-size: 14px;'> <a><a/>"))
                .isEqualTo("<br /> text here <br /> text here <br /> text here <br style='font-size: 14px;' /> <a><a/>");
    }

    @Test
    void closeBrTagsDoubleQuotes() {
        assertThat(StringReplaceBr.closeBrTags("<br> text here <br/> text here <br> text here <br style=\"font-size: 14px;\"> <a><a/>"))
                .isEqualTo("<br /> text here <br /> text here <br /> text here <br style=\"font-size: 14px;\" /> <a><a/>");
    }

    @Test
    void closeBrSmall() {
        assertThat(StringReplaceBr.closeBrTags("<br/> <br> <br/> <a><a/> <br wow=''>"))
                .isEqualTo("<br /> <br /> <br /> <a><a/> <br wow='' />");
    }

}
Sign up to request clarification or add additional context in comments.

4 Comments

Extra points for including a unit test. I would expand that unit test with a few more cases, though.
I'd add " in case <br style="something"> occurs
@ChristopherSchultz thank you so much for your comment and your pointers. I have just expanded the unit tests :)
@wojteo thank you too for your comment and pointers. I have just added support with double quotes. :)

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.