简体   繁体   English

Javascript正则表达式,删除单段换行符

[英]Javascript regex, make remove single paragraph line breaks

I've got text in this format: 我有这种格式的文字:

word word,
word word.

word word
word word.

Not specific to that two word format, it's just a line break before so many characters, rather than one long string of paragraph. 不是特定于那两个单词格式,它只是在这么多字符之前的换行符,而不是一个长串的段落。 But I'm trying to get it to be that one long string of paragraph. 但我试图让它成为一段长长的段落。 So it should look like this: 所以看起来应该是这样的:

word word, word word.
word word word word.

If I use the code text.replace(/$\\n(?=.)/gm, " ") and output that to the terminal I get text that looks like: 如果我使用代码text.replace(/$\\n(?=.)/gm, " ")并将其输出到终端,我会得到如下所示的文本:

 word word, word word.
 word word word word.

It's got an extra space at the start of the paragraph, but that's good enough for what I'm trying to do (although if there's also a way to remove it in one replace function than that's good). 它在段落的开头有一个额外的空间,但这对我正在尝试做的事情已经足够好了(尽管如果还有一种方法可以在一个替换函数中删除它而不是那个好的)。 The problem is that when I output it to a textarea it doesn't remove the \\n character, and I just get text that looks like this: 问题是,当我将它输出到textarea时,它不会删除\\ n字符,我只是得到如下所示的文本:

 word word,
 word word.

 word word
 word word.

I'm trying to do this all client side, currently running it in Firefox. 我试图在所有客户端执行此操作,目前在Firefox中运行它。

I'm not the best with regex, so this might be really simple and I'm just ignorant on how to do it. 我不是最好的正则表达式,所以这可能非常简单,我只是不知道如何做到这一点。 But any help would be really appreciated. 但任何帮助都会非常感激。 Thanks! 谢谢!

回车是\\ r \\ n所以你需要使用

 text.replace(/$(\\r|\\n)(?=.)/gm, " "); 

You probably missed some \\r, here's a way to match all sort of new lines and not have extra spaces: 你可能错过了一些\\ r \\ n,这里有一种方法可以匹配所有类型的新行并且没有额外的空格:

 var input = 'word word,\\nword word.\\n\\nword word\\nword word.'; // split if 2 or more new lines var out = input.split(/(\\r\\n|\\n|\\r){2,}?/) // split the paragraph by new lines and join the lines by a space .map((v) => v.split(/\\r\\n|\\n|\\r/).join(' ')) // there is some spaces hanging in the array, filter them .filter((v) => v.trim()) // join together all paragraphs by \\n .join('\\n'); $('#txt').append(out); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <textarea id="txt"></textarea> 

Below a snippet of code that satisfy your request, i've removed the leading whitespaces too (caused by empty lines), using a closure with the replace function: 在满足您的请求的代码片段下方,我已经删除了前导空格(由空行引起),使用带有replace函数的闭包:

 var regex = /([^.])\\s+/g; var input = 'word word,\\nword word.\\n\\nword word\\nword word.'; var result = input.replace(regex, function(all, char) { return (char.match(/\\s/)) ? char : char + ' ' ; }); document.write('<b>INPUT</b> <xmp>' + input + '</xmp>'); document.write('<b>OUTPUT</b> <xmp>' + result + '</xmp>'); 

Regex Breakout 正则表达式突围

([^.])        # Select any char that is not a literal dot '.'
              # and save it in group $1
\s+           # 1 or more whitespace char, remove trailing spaces (tabs too)
              # and all type of newlines (\r\n, \r, \n)

NOTE 注意

if for some reason you want to keep the leading whitespace, simplify the code below as follow: 如果由于某种原因你想保留前导空格,请简化下面的代码,如下所示:

 var regex = /([^.])\\s+/g; var replace = '$1 '; var input = 'word word,\\nword word.\\n\\nword word\\nword word.'; var result = input.replace(regex, replace); document.write('<b>INPUT</b> <xmp>' + input + '</xmp>'); document.write('<b>OUTPUT</b> <xmp>' + result + '</xmp>'); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM