Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 10 years ago.
尝试这样的操作:
$('br').text('\n').contents().unwrap();
首先,它向 \n 的所有 br 元素添加一个文本节点子节点。然后它会打开它们,只留下下面的文本。您可以在此处查看它的实际效果:
\n
http://jsfiddle.net/UgZNm/
Try something like this:
First it adds a text node child to all br elements of \n. Then it unwraps them leaving only the text below it. You can see it in action here:
我找到了这个正则表达式,它应该可以解决各种格式问题:
function br2nl(input) { return input.replace(/<[bB][rR](\s+)?\/?>/g, "\n"); }
I found this regex, which should take care of all kinds of formatting problems:
在没有实际 HTML 解析器的情况下解析 HTML 可能是一个坏主意,但正则表达式通常就足够了。
text.replace(/<br[^>]*>/gi, "\n");
这将替换、、、 等。
但是,它不会取代 ,以防您可能有
。如果是这样,您可以尝试以下操作:
text.replace(/<\/?br[^>]*>/gi, "\n");
如果您不介意
变成 \n\n。
Parsing HTML without an actual HTML parser might be a bad idea, but regular expressions can often be Good Enough.
This will replace <br>, <br/>, <br />, <br some_attribute="value" />, etc etc.
<br/>
<br />
<br some_attribute="value" />
However, it will not replace </br>, in case you might have <br></br>. If so, you could try this:
</br>
<br></br>
if you don't mind <br></br> getting turned into \n\n.
我认为 replaceWith 是最好的解决方案,而且清晰简单:
replaceWith
$(selector + ' br').replaceWith('\n');
这是一个显示此操作的小提琴: http://jsfiddle.net/hHJvM/
I think replaceWith is the best solution, as well as being clear and easy:
Here's a fiddle that shows this in action: http://jsfiddle.net/hHJvM/
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
暂无简介
文章 0 评论 0
接受
发布评论
评论(4)
尝试这样的操作:
首先,它向
\n
的所有 br 元素添加一个文本节点子节点。然后它会打开它们,只留下下面的文本。您可以在此处查看它的实际效果:http://jsfiddle.net/UgZNm/
Try something like this:
First it adds a text node child to all br elements of
\n
. Then it unwraps them leaving only the text below it. You can see it in action here:http://jsfiddle.net/UgZNm/
我找到了这个正则表达式,它应该可以解决各种格式问题:
I found this regex, which should take care of all kinds of formatting problems:
在没有实际 HTML 解析器的情况下解析 HTML 可能是一个坏主意,但正则表达式通常就足够了。
这将替换
、
、
、
等。但是,它不会取代
,以防您可能有。如果是这样,您可以尝试以下操作:
如果您不介意
变成 \n\n。
Parsing HTML without an actual HTML parser might be a bad idea, but regular expressions can often be Good Enough.
This will replace <br>,
<br/>
,<br />
,<br some_attribute="value" />
, etc etc.However, it will not replace
</br>
, in case you might have<br></br>
. If so, you could try this:if you don't mind
<br></br>
getting turned into \n\n.我认为
replaceWith
是最好的解决方案,而且清晰简单:这是一个显示此操作的小提琴: http://jsfiddle.net/hHJvM/
I think
replaceWith
is the best solution, as well as being clear and easy:Here's a fiddle that shows this in action: http://jsfiddle.net/hHJvM/