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
.
In the opening Find and Replace dialog box, click on the Find what box and press the Ctrl + Shift + J keys together, enter br into the Replace with box, and then click the Replace All button.
replaceWith( newContent )Returns: jQuery. Description: Replace each element in the set of matched elements with the provided new content and return the set of elements that was removed.
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"));
example
edit: added i
flag
edit2: you can use /<br[^>]*>/gi
which will match anything between the br
and slash
if you have for example <br class="clear" />
myString.replace(/<br ?\/?>/g, "\n")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With