0

I have several paragraphs of text that are being stripped of all of their formatting by a javascript function.

I have the function doing 99% of what I need it to do already, with one minor problem.

At the very end of the text it is putting two <br><br> tags that I do not want as it just adds blank white space at the end. In other areas of the text there are double <br> tags I want to leave in place.

So my question is how do I take the entire block of text and only remove the very last <br><br> tags?

2

1 Answer 1

4

How about using regular expressions:

var text = '<br><br> Hello there... <br><br>\n<p>How are you?</p><br><br>';
text = text.replace(/<br><br>$/, '');

the $ checks to make sure you only remove the one at the end of the string.

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

2 Comments

The global /g flag is not necessary, when you're just replacing/removing one occurrence. text.replace(/<br><br>$/, '') should be enough
also, you should be doing text = text.replace(/<br><br>$/, ''); or some var result = text.replace(/<br><br>$/, ''); so that text, result has the resulting string.

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.