开发者

Regex with dynamic <textarea>

开发者 https://www.devze.com 2022-12-29 14:51 出处:网络
How can I do this with the JS replace() method: Make \\n\\n change to <p>$1</p> Change single \\n to <br>

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")

to Victor, and others,

I tried this code to convert it back, but it gave me this in return (the ... is just a lot more text)

$(this).html().replace(/\n/g, "<br>").replace(/<br><br>(.*)?/g, "<p>$1</p>");

gave me:

<div class="editable" data-name="notes-content" data-type="textarea">
“Time Certain” indicates that an item will not be heard by Council prior to the time certain
.<p>Communications items are three minutes each开发者_如何学C. ... 
<br><br>The * indicates an emergency ... 
<br><br>Check our Web site: www.portlandonline.com
<br>
</p>
</div>

If you notice, it didnt wrap the first line, and its not wrapping them in <p>s, just the entire thing, i need it all in <p>s


How about this (version #4):

$(this).html().replace(/\n/g, "<br>").replace(/(.+?)<br><br>/g, "<p>$1</p>");


Here you go:

$(this).html().replace(/\n/g, "<br>").replace(/<br><br>(.*)?/g, "<p>$1</p>");
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号