82

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.

1
  • which server side language u r using?? if php then nl2br() is the function u need Commented May 11, 2011 at 4:54

5 Answers 5

203
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" />

Sign up to request clarification or add additional context in comments.

4 Comments

This works but i just noticed IE6,7,8 displays capital <BR> How can we do it so it works with both lower and upper case letters.
I think supposed to be put inside a textarea.
@Pinkie you can add i flag for case insensitive
@pinkie replace the br in the regex with [bB][rR] or add an i after the g
15

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

2 Comments

it will not work if there are more than one space between the br and the slash
@teneff There isn't normally more than one space but that can be easily solved with a *
8

True jQuery way if you want to change directly the DOM without messing with inner HTML:

$('#text').find('br').prepend(document.createTextNode('\n')).remove();

Prepend inserts inside the element, before() is the method we need here:

$('#text').find('br').before(document.createTextNode('\n')).remove();

Code will find any <br> elements, insert raw text with new line character and then remove the <br> elements.

This should be faster if you work with long texts since there are no string operations here.

To display the new lines:

$('#text').css('white-space', 'pre-line');

2 Comments

That call to remove() removes the newly created textNode. next() would have to be called priorly since prepend() returns the prepended element. So it would have to be: >>>>$('#text').find('br').prepend(document.createTextNode('\n')).next().remove();<<<< Still +1 though. =)
@Clox: prepend returns the original selector, see jsfiddle.net/nothrem/ygjb5z0h . But what may be the problem is that prepend() inserts the new element inside the old one so when you remove the old one after, it removes the new one as well. Correct method to use is before().
5

a cheap and nasty would be:

jQuery("#myDiv").html().replace("<br>", "\n").replace("<br />", "\n")

EDIT

jQuery("#myTextArea").val(
    jQuery("#myDiv").html()
        .replace(/\<br\>/g, "\n")
        .replace(/\<br \/\>/g, "\n")
);

Also created a jsfiddle if needed: http://jsfiddle.net/2D3xx/

4 Comments

needs to pass "g" as third arg to replace
@adam Opps! missed that. Thanks adam. Also the regex in your answer is more robust.
Sometimes simple mistake happens like : Some one can use "</br>" , in this case use replace(/<\s*\/?br>/ig, "\r\n")
Hi, if like me you inherit a text in html with the line feeds returned formatted by the nl2br () function of PHP you get line feeds like this: <br />. For me, part of this solution worked: (in javascript, on an object $ .text_item) $ .text_item.replace (/ \ <br \/\> / g, "\ n"); , it allowed me to display this text dynamically with Mustache for a template. Line breaks are well rendered in the <textarea>, I voted for!
2

Not really anything to do with jQuery, but if you want to trim a pattern from a string, then use a regular expression:

<textarea id="ta0"></textarea>
<button onclick="
  var ta = document.getElementById('ta0');
  var text = 'some<br>text<br />to<br/>replace';
  var re = /<br *\/?>/gi;
  ta.value = text.replace(re, '\n');
">Add stuff to text area</button>

2 Comments

Sometimes simple mistake happens like : Some one can use "</br>" , in this case use replace(/<\s*\/?br>/ig, "\r\n")
@eegloo—</br> is a straightforward error. Strictly, it will close any preceding opening tag however browsers seem to be stupidly tolerant and convert it to <br>. Also, the OP hasn't asked for that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.