3
"test<br>test<br>test<br>test".replace('/<br>/g', '\n');

Does not replace the <br>'s with \n, it leaves the string unchanged. I can't figure out why.

Boyd
  • 723
  • 4
  • 15
  • 32
  • 6
    `'/
    /g'` is a string, not a regex. Lose the quotes. It's looking for the *literal* string `'/
    /g'`. You want `.replace(/
    /g, '\n');`. JavaScript has RegEx literals.
    – gen_Eric Apr 07 '14 at 17:51
  • 1
    duplicate of http://stackoverflow.com/questions/8062399/how-replace-html-br-with-newline-character-n – Mostafa Berg Apr 07 '14 at 17:52

2 Answers2

15

Because you're passing the regex object as a string instead of a regex. Remove the '' from the first argument you're passing to replace()

jxpx777
  • 3,632
  • 4
  • 27
  • 43
12

You need to use a Regex literal, not a string:

"test<br>test<br>test<br>test".replace(/<br>/g, '\n');
SomeKittens
  • 38,868
  • 19
  • 114
  • 143