【发布时间】:2010-05-03 20:05:32
【问题描述】:
如何使用 JS 的 replace() 方法做到这一点:
- 将\n\n更改为
<p>$1</p> - 将单个\n 更改为
<br> - 然后再回来。我想我有这部分,看底部的JS。
示例 HTML:
<p>Hello</p><p>Wor<br>ld</p>
<textarea> 看起来像:
Hello
Wor
ld
那么,我该如何实现呢?这是一个 AJAX 表单,当您单击此 div 时,它会更改为 <textarea> 并返回,然后是第四个等等。所以,我需要它从 <p>s 和 <br>s 转到 \n\n 和 @ 987654330@。对于 to <textarea> from HTML 我有:
$(this).html().replace(/\s?<\/?(p|br\s?\/?)>\s?/g,"\n")
致维克多和其他人,
我尝试使用此代码将其转换回来,但它给了我这个作为回报(... 只是更多的文本)
$(this).html().replace(/\n/g, "<br>").replace(/<br><br>(.*)?/g, "<p>$1</p>");
给了我:
<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. ...
<br><br>The * indicates an emergency ...
<br><br>Check our Web site: www.portlandonline.com
<br>
</p>
</div>
如果你注意到,它没有包装第一行,也没有将它们包装在 <p>s 中,只是整个事情,我需要在 <p>s 中进行所有操作
【问题讨论】:
标签: javascript html ajax regex dynamic