0

I have a string in my Javascript application which looks like this

 var myMessage = 'Thats the first line<br /> thats the second line<br /> thats the third line.'; 

Now I have tried to replace all <br /> with \n and I have tried this command:

var messageFormatted = myMessage.replace('<br />', ' '); 

but in messageFormatted there are still the <br />s. My question now would be what I am doing wrong?

4
  • Is there any other HTML in the text that needs to stay? Commented Aug 25, 2016 at 22:14
  • 2
    To perform a global search and replace, include the g switch in the regular expression. developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Aug 25, 2016 at 22:14
  • var messageFormatted = myMessage.replace(/<br \/>/g, '\n'); Commented Aug 25, 2016 at 22:15
  • 1
    it's tricky because often this is not really a place for "regex" or "regex-like" patterns. usually one would be advised to parse the html and then remove the br elements. Commented Aug 25, 2016 at 22:19

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.