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?

Sherwin Flight
  • 2,345
  • 7
  • 34
  • 54
  • possible duplicate of [Delete Last Char of String with Javascript](http://stackoverflow.com/questions/1990265/delete-last-char-of-string-with-javascript) not exactly the same, but perhaps can be extrapolated from. maybe i'm wrong though. in which case, kindly ignore/delete this comment. thanks – mechanical_meat Mar 31 '12 at 07:59
  • 1
    Possible duplicate of http://stackoverflow.com/questions/3597611/javascript-how-to-remove-characters-from-end-of-string – Stefan Mar 31 '12 at 07:59

1 Answers1

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.

Cameron
  • 1,675
  • 11
  • 12
  • The global /g flag is not necessary, when you're just replacing/removing one occurrence. `text.replace(/

    $/, '')` should be enough
    – Saebekassebil Mar 31 '12 at 08:07
  • also, you should be doing text = text.replace(/

    $/, ''); or some var result = text.replace(/

    $/, ''); so that text, result has the resulting string.
    – Nilesh Mar 31 '12 at 08:14