[英]How can I detect newline that is not followed by another newline?
/\r\n|\r|\n/
And will be replaced with \\n\\n
. 并且将被替换为\\n\\n
。 All I'm trying to do is replacing all single-newlines with double-newlines. 我要做的就是用双换行符替换所有单换行符。
So the input: 所以输入:
something
something else
The expected result: 预期结果:
something
something else
The problem is, I want to avoid matching new-lines that are followed by another (or more) new lines. 问题是,我想避免匹配其他(或更多)新行之后的新行。 So I want to just select the single-newlines. 所以我只想选择单换行符。 So this shouldn't have any matches: 所以这应该没有任何匹配:
something
something else
Any idea how can I do that? 知道我该怎么做吗?
Use ([^\\r\\n])\\n([^\\r\\n])
and replace it with $1\\n\\n$2
. 使用([^\\r\\n])\\n([^\\r\\n])
并将其替换为$1\\n\\n$2
。 This means 这意味着
(
= start group 1 (
=开始第1组
[^
= start negated set (NOT) [^
=起始否定集(NOT)
\\n\\r
= the characters not to match \\n\\r
=字符不匹配
]
= end negated set ]
=求反集
)
= end set 1 )
=终端机1
\\n
= match newline \\n
=匹配换行符
Set 2 is a repeat of set 1. 组2是组1的重复。
Replaces with 替换为
$1
= first set $1
=第一套
\\n\\n
= your characters \\n\\n
=您的角色
$2
= second set $2
=第二组
Use regex ([^\\r\\n])[\\r\\n](?![\\r\\n]+)
and replace with $1\\n\\n
. 使用正则表达式([^\\r\\n])[\\r\\n](?![\\r\\n]+)
并替换为$1\\n\\n
。 It uses a capture group to capture the symbol in front of newline that is not newline itself, and a negative lookahead that makes sure that newline is not followed by another newline without capturing the next symbol. 它使用捕获组来捕获不是换行符本身的换行符前面的符号,并使用否定的前瞻性确保在不捕获下一个符号的情况下,换行符不会跟随另一个换行符。
Unfortunately, JS currently doesn't fully support negative lookbehind (it was accepted in ECMAScript 2018), otherwise it could be possible to use (?<![\\r\\n])[\\r\\n](?![\\r\\n])
– no capture groups at all, so replacing string would be just \\n\\n
. 不幸的是,JS当前不完全支持负向后看(在ECMAScript 2018中已被接受),否则可以使用(?<![\\r\\n])[\\r\\n](?![\\r\\n])
–根本没有捕获组,因此替换字符串只是\\n\\n
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.