javascript 将新行转换为段落/br HTML 标签,这可以是单个正则表达式吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8579093/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting new lines to paragraph/br HTML tags, can this be a single regex?
提问by rr.
An app I am working on has the user enter content in plaintext that will later be displayed as HTML. To make the user's content display as nicely as possible, we transform the content as follows:
我正在开发的一个应用程序让用户以纯文本形式输入内容,稍后将显示为 HTML。为了让用户的内容尽可能的好看,我们对内容进行如下改造:
Any blocks of text delimited by two or more new line characters are wrapped in <p> tags. The new line characters (and any whitespace in-between) are stripped out.
Any single new line characters (along with surrounding whitespace) are replaced by a <br /> tag.
由两个或多个换行符分隔的任何文本块都包含在 <p> 标签中。新行字符(以及中间的任何空格)被去除。
任何单个换行符(连同周围的空格)都被 <br /> 标签替换。
I'm currently achieving this by putting the the text through two regex replacements but was wondering if it could possibly be consolidated to one. Here's what I have right now (JavaScript):
我目前正在通过将文本通过两个正则表达式替换来实现这一目标,但想知道是否可以将其合并为一个。这是我现在拥有的(JavaScript):
// content holds the text to process
content = '<p>' + content.replace(/\n([ \t]*\n)+/g, '</p><p>')
.replace(/\n/g, '<br />') + '</p>';
回答by Vadzim
There are two different replacement strings. So it's impossible to make it in single replace call.
有两种不同的替换字符串。所以不可能在单个替换调用中实现它。
But second replace() can be changed to more effective plain string substitution.
但是可以将第二个 replace() 更改为更有效的纯字符串替换。
content = '<p>' + content.replace(/\n([ \t]*\n)+/g, '</p><p>')
.replace('\n', '<br />') + '</p>';
So, there's single regexp. :)
所以,只有一个正则表达式。:)
Update
更新
Javascript bluffed me. String replace
handles only first occurrence.
Javascript 欺骗了我。字符串replace
仅处理第一次出现。
See How to replace all occurrences of a string in JavaScript?
There are also given several possible workaround implementations of replaceAll
.
还给出了replaceAll
.
回答by nobug
I was facing a similar requirement - to mimic the wpautop functionality into javascript (for use in the theme customizer).
我面临着类似的要求 - 将 wpautop 功能模拟到 javascript 中(用于主题定制器)。
So here is my approach to the problem:
所以这是我解决问题的方法:
First, replace the case when there are two line breaks.
首先,当有两个换行符时更换外壳。
to = to.replace(/\n{2}/g, ' </p><p>');
Then, replace the case when there are only one line breaks left.
然后,当只剩下一个换行符时更换外壳。
to = to.replace(/\n/g, ' <br />');
And lastly wrap the whole content in a <p> tag
最后将整个内容包裹在一个 <p> 标签中
to = '<p>' + to + '</p>';
I used the because my styling implied margin after the paragraph. You can omit them if you don't need them. Also, one drawback is that the single line breaks are inserted in the beginning of the next paragraph, but this is something I can live with that.
我使用了 因为我的样式暗示了段落后的边距。如果你不需要它们,你可以省略它们。此外,一个缺点是在下一段的开头插入了单个换行符,但这是我可以接受的。