views:

867

answers:

3

I am using a simple regex to replace break tags with newlines:

br_regex = /<br>/;

input_content = input_content.replace(br_regex, "\n");

This only replaces the first instance of a break tag, but I need to replace all. preg_match_all() would do the trick in php, but I'd like to know the javascript equivalent. Thanks!

+10  A: 

use the global flag, g:

foo.replace(/<br>/g,"\n")

annakata
you beat me by 1 second.
Triptych
float like a butterfly, sting like VB..
annakata
downvote wtf - in what possible way is "use g" wrong?
annakata
A: 

JS idiom for non-Regexp global replace:

input_content.split('<br>').join('\n')
bobince
A: 

Just a note it's probably wiser to use \r\n instead of just \n

Andrew G. Johnson