javascript jquery删除新行然后用块元素包装文本节点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5020434/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 15:41:25  来源:igfitidea点击:

jquery remove new line then wrap textnodes with block element

javascriptjqueryregex

提问by angry kiwi

I have some paragraphs like this:

我有一些这样的段落:

"This is the first para. r\r\n\n This is the second one with lot of new line after \n\n\n\n\n\n And the last para. \n\r\r"

"这是第一段。r\r\n\n 这是第二段,\n\n\n\n\n\n 和最后一段。\n\r\r"

I want to remove the new lines and wrap each paragraph with the <p>tag. I'm expecting output as follows:

我想删除新行并用<p>标签包裹每个段落。我期待输出如下:

<p>This is the first para.</p>
<p>This is the second one with lot of new line after</p>
<p>And the last para.</p>

回答by Brad Christie

var d = "line 1\n\nline2\n\n\n\nline3";
$('body').append('<p>'+d.replace(/[\r\n]+(?=[^\r\n])/g,'</p><p>')+'</p>');

something like this perhaps?

也许是这样的?

If you find the line contains any new lines/carriage returns at the beginning or end, remember to call a .trim()on the string first before replacing the values (using this example, d.trim().replace(...))

如果您发现该行在开头或结尾包含任何新行/回车符,请记住.trim()在替换值之前先在字符串上调用 a (使用此示例,d.trim().replace(...)

More robust solution

更强大的解决方案

function p(t){
    t = t.trim();
    return (t.length>0?'<p>'+t.replace(/[\r\n]+/,'</p><p>')+'</p>':null);
}
document.write(p('this is a paragraph\r\nThis is another paragraph'));


PHP Version:

PHP版本:

$d = "paragraph 1\r\nparagraph 2\r\n\r\n\r\nparagraph3";
echo "<p>".preg_replace('/[\r\n]+/','</p><p>',$d)."</p>";

http://www.ideone.com/1TRhh

http://www.ideone.com/1TRhh

回答by lonesomeday

How about this?

这个怎么样?

var output = $();

$.each("This is the first para. \r\n\n This is the second one with lot of new line after \n\n\n\n\n\n And the last para. \n\r\r".split(/[\n\r]+/g), function(i, el) {
    if (el) {
        output = output.add($('<p>' + el + '</p>'));
    }
});

This creates a jQuery selection containing the pelements.

这将创建一个包含p元素的 jQuery 选择。

回答by lonesomeday

replace regex: /(?:[\r\n]*)(.+)(?:[\r\n]*)/
with: <p>$1</p>
context: global

将正则表达式:替换 /(?:[\r\n]*)(.+)(?:[\r\n]*)/
为:<p>$1</p>
上下文:全局

In Perl its: s/ (?:[\r\n]*) (.+) (?:[\r\n]*) /<p>$1<\/p>/xg

在 Perl 中: s/ (?:[\r\n]*) (.+) (?:[\r\n]*) /<p>$1<\/p>/xg

回答by helle

you can try something like this:

你可以尝试这样的事情:

function parse_it(yourstring, tagname){
   yourstring.replace(/[\r\n]+/g, '#x#');
   paragraphs = yourstring.split('#x#');

   pars = "";
   for(var i=0; i<paragraphs.length; i++)
      pars += '<'+tagname+'>'+paragraphs[i]+'</'+tagname+'>';

   return pars;
}

var parsedstring = parse_it("This is the first para. r\r\n\n This is the second one with lot of new line after \n\n\n\n\n\n And the last para. \n\r\r", 'p');