How do i write a regex to replace <br />
or <br>
with \n
. I'm trying to move text from div to textarea, but don't want <br>
's to show in the textarea, so i want to replace then with \n
.
How do i write a regex to replace <br />
or <br>
with \n
. I'm trying to move text from div to textarea, but don't want <br>
's to show in the textarea, so i want to replace then with \n
.
var str = document.getElementById('mydiv').innerHTML;
document.getElementById('mytextarea').innerHTML = str.replace(/<br\s*[\/]?>/gi, "\n");
or using jQuery:
var str = $("#mydiv").html();
var regex = /<br\s*[\/]?>/gi;
$("#mydiv").html(str.replace(regex, "\n"));
edit:增加了i
个旗帜
edit2: you can use /<br[^>]*>/gi
which will match anything between the br
and slash
if you have for example <br class="clear" />