JavaScript equivalent of PHP's preg_replace

PhpJavascriptRegex

Php Problem Overview


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.

Php Solutions


Solution 1 - Php

Use the global flag, g:

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

Solution 2 - Php

JS idiom for non-Regexp global replace:

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionryonlifeView Question on Stackoverflow
Solution 1 - PhpannakataView Answer on Stackoverflow
Solution 2 - PhpbobinceView Answer on Stackoverflow