title: jQuery - replace line break <br /> with new line \n
body: <p>I am using jQuery 1.11.</p>
  
  <p>I have a string displayed in html and I want that same string copied and pasted to a textarea.</p>
  
  <p>The problem for me is that the string to be pasted contains a line break <code>&lt;br /&gt;</code>. To correctly display the string in the textarea, I must replace the line break <code>&lt;br /&gt;</code> with a new line <code>\n</code>.</p>
  
  <p>I am using <code>replace</code>, but each permutation I write to replace the <code>&lt;br /&gt;</code> with the <code>\n</code> fails. The string is printed along one line.</p>
  
  <p>I have searched google &amp; SO, but this just does not work for me - I am missing something basic here.</p>
  
  <p>How can I write a string replace function to replace the <code>&lt;br /&gt;</code> with <code>\n</code>?</p>
  
  <p>Here is my string:</p>
  
  <pre><code>[employer]&lt;br /&gt;[mm/yyy] - [mm/yyy] - ([number] years1, [number] months)&lt;br /&gt;&lt;br /&gt;
  </code></pre>
  
  <p>Here is the replace code that does not work for me:</p>
  
  <pre><code>var text = $(this).closest('.employment_history_suggestion_add_button').prev().text();  // Get text of the previous span element
  text = text.replace(/&lt;br\s*\/?&gt;/mg,"\n");  // replace line break with new line for correct display in textarea.
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p><strong>This is the code that now works.</strong></p>
  
  <p>I was using the <code>.text()</code> - should have been using the <code>.html()</code>.</p>
  
  <p>I was using <code>\\n</code> - should have been using <code>\n</code>.</p>
  
  <pre><code>var text = $(this).closest('.employment_history_suggestion_add_button').prev().html();  // Get html of the copied string (keep the line breaks for replacing with new lines).
  text = text.replace(/&lt;br\s*\/?&gt;/gim, "\\n");
  $('#id_employment_history_employerTA').val(function (i, oldVal) {
                  return oldVal + text;  // append the value of text to the textarea.
  });
  </code></pre>
  
  <p>I hope that this helps someone.</p>