ryonlife
ryonlife

Reputation: 6623

JavaScript equivalent of PHP's preg_replace

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.

Upvotes: 73

Views: 96795

Answers (2)

annakata
annakata

Reputation: 75814

Use the global flag, g:

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

Upvotes: 135

bobince
bobince

Reputation: 536389

JS idiom for non-Regexp global replace:

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

Upvotes: 18

Related Questions