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?

trincot
  • 317,000
  • 35
  • 244
  • 286
quma
  • 5,233
  • 26
  • 80
  • 146
  • Is there any other HTML in the text that needs to stay? – trincot Aug 25 '16 at 22:14
  • 2
    To perform a global search and replace, include the g switch in the regular expression. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace – Xotic750 Aug 25 '16 at 22:14
  • `var messageFormatted = myMessage.replace(/
    /g, '\n');`
    – Wiktor Stribiżew Aug 25 '16 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. – bitten Aug 25 '16 at 22:19

0 Answers0