Boyd
Boyd

Reputation: 763

Why is replace('<br>/g', '\n') not working

"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.

Upvotes: 3

Views: 722

Answers (2)

jxpx777
jxpx777

Reputation: 3641

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()

Upvotes: 15

SomeKittens
SomeKittens

Reputation: 39512

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

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

Upvotes: 12

Related Questions