在不以特殊字符开头的行中匹配换行符

问题描述 投票:0回答:1

我想匹配所有\n\r,但当行以@开头时除外我有这个正则表达式:(?!^@.*\r*\n*)(\r*\n*)例如:

@text text text \r\n
@text text text\r\n
The line break after this text should be matched \n
The line break after this text also should be selected \r\n
\n\r
@this line break should not be selected\r\n
\n\r

因此,唯一应选择的换行符是第3、4、5和7号有什么想法吗?

javascript regex regex-lookarounds
1个回答
0
投票

喜欢吗?

const text = document.getElementById("x").innerText;
document.getElementById("x").innerText=text.replace(/(^[^@].*)(\n?\r?)/gm,"$1XX")
#x { font-family: "Courier New", monospace;
  white-space: pre; }
<div id="x">@text text text \n\r
@text text text\n\r
The line break after this text should be matched \n
The line break after this text also should be selected \n\r
\n\r
@this line break should not be selected\n\r
\n\r</div>
© www.soinside.com 2019 - 2024. All rights reserved.