Javascript jquery删除新行,然后用block元素包装textnodes

Javascript jquery删除新行,然后用block元素包装textnodes,javascript,jquery,regex,Javascript,Jquery,Regex,我有以下几段: 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]+'</'+t

我有以下几段:

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');
“这是第一段。r\r\n\n 是第二个有很多新线路的 在\n\n\n\n\n\n和最后一段之后。 \n\r\r“

我想删除新行,并用
标记将每个段落包装起来。我期望输出如下:

<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>
这是第一段

这是第二个有很多新的生产线后

最后一段


PHP版本:

$d = "paragraph 1\r\nparagraph 2\r\n\r\n\r\nparagraph3";
echo "<p>".preg_replace('/[\r\n]+/','</p><p>',$d)."</p>";
$d=“段落1\r\n段落2\r\n\r\n\r\n段落3”;
echo“”。preg_replace(“/[\r\n]+/”、“

”、$d)。“

”;

您可以尝试以下方法:

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');
函数解析它(你的字符串,标记名){
yourstring.replace(/[\r\n]+/g,“#x#”);
段落=字符串.split('#x#');
pars=“”;
对于(var i=0;i

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>'));
    }
});
var输出=$();
$.each(“这是第一个段落。\r\n\n这是第二个段落,在\n\n\n\n\n之后有很多新行,最后一个段落。\n\r\r”。拆分(/[\n\r]+/g),函数(i,el){
如果(el){
output=output.add($(''+el+'

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

替换正则表达式:
/(?:[\r\n]*)(.+)(?:[\r\n]*)/

带:
$1


背景:全球


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

这是您真正需要的还是仅仅是一个示例?我的意思是是否会有更多的换行符等…任何换行符都应该转换为或?这是一个示例,可能会有更多或更少的换行符。任何看起来像段落的东西都会转换为

\r并且其他控制字符也会被转换为在HTML中被忽略。它们在字符串中是有效的(如果转义的话),我认为这不会正常工作。行制动器以'r\'开头,因为它会将第一个r作为文本的一部分,对吗?@Trufa:the
[\r\n]
表示新行或回车,下面的
+
表示重复1次以上。这将处理任何一个订单或任何连续的订单。(例如
\r
\n
\r\n\n
`\n\r\n`等)@Trufa:为什么它会在意?“不是回车字符,它是字符串的一部分。如果单词以r结尾,例如
harder\r\n\r\n aster\r\nstronger
@runforest和@Brad my bad对这两个字符都不好,那我太傻了!正如Brad解释的那样,我的示例无效,因为r\\n不是回车字符,我被OP示例弄糊涂了,因为它显示
r\r\n\n
但这实际上是一个“输入错误”或错误,所以brad和runrun,我的错!顺便问一下@runrun你同意这应该被修复吗?如果
var d=“”
”\n\n“
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>'));
    }
});