Robert M.
Robert M.

Reputation: 1367

Cut <br/>-Tags from String end

I am currently developing a Web-Application using Java EE where I'm using a Rich-Javascript-Editor (http://www.primefaces.org/showcase/ui/editor.jsf). As the user can easily add too many linebreaks that will be convertet to linebreak-tags, I need to remove all these Tags from the end of a String.

Is there an elegant way of using Regex to accomplish this?

An example String would be:

"This is a test <b>bold</b><br/><br/>" 

Where obviously the last two tags have to be removed.

Thank you in advance for any help

Best Regards, Robert

Upvotes: 2

Views: 3582

Answers (4)

Prashant Singh
Prashant Singh

Reputation: 11

This should do the trick for most cases.

This regex considers most of scenarios where different forms for <br> occurs

All of the following are valid <br> statements :

-<br>
-<br/>
-<br />
-<br / >
-<br/ >
-<br //// >

This regex identifies all such forms of <br>

string = string.replaceAll("<br[\\s/]*>", "");

Upvotes: 1

madhu527
madhu527

Reputation: 4782

Try this one.Its working for me

myString =myString .replaceAll("<br/>", "");

Upvotes: 0

maerics
maerics

Reputation: 156424

Something like this:

String s = "This is a test <b>bold</b><br/><br/>";
String s2 = s.replaceAll("(\\s*<[Bb][Rr]\\s*/?>)+\\s*$", "");
// s2 = "This is a test <b>bold</b>";

Note that it will also remove trailing whitespace; you can delete the final \\s* if you don't want that.

Upvotes: 7

super_aardvark
super_aardvark

Reputation: 1855

Here is a simple line of Java code to remove all instances of the substring "<br/>" from the end of a string myString:

myString = myString.replaceAll("(<br/>)+$", "");

Upvotes: 3

Related Questions