Regex with dynamic <textarea>

Posted by Oscar Godson on Stack Overflow See other posts from Stack Overflow or by Oscar Godson
Published on 2010-05-03T20:05:32Z Indexed on 2010/05/03 20:08 UTC
Read the original article Hit count: 276

Filed under:
|
|
|
|

How can I do this with the JS replace() method:

  • Make \n\n change to <p>$1</p>
  • Change single \n to <br>
  • Then back again. I think I have this part, see the JS at the bottom.

Example HTML:

<p>Hello</p><p>Wor<br>ld</p>

The <textarea> would look like:

Hello

Wor
ld

So, how can I achieve this? It's an AJAX form where when you click on this div it changes to a <textarea> and back, and fourth, etc. So, I need to it to go from <p>s and <br>s to \n\n and \n. For the going to <textarea> from HTML I have:

$(this).html().replace(/\s?<\/?(p|br\s?\/?)>\s?/g,"\n")

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about regex