我想匹配所有 \n
或 \r
,除了以 @
开头的行之外
我有这个正则表达式:
(?!^@.*\r*\n*)(\r*\n*)
例如:
@text text text
@text text text
The line break after this text should be matched
The line break after this text also should be selected
@this line break should not be selected
因此,唯一应该选择的换行符是第 2 个、第 4 个、第 5 个、第 6 个和第 7 个中的换行符。 有什么想法吗?
最佳答案
像这样吗?
https://regex101.com/r/bYIpiQ/2
注意我不选择文本,我替换\n\r或\n或\r
const text = document.getElementById("x").innerText;
document.getElementById("x").innerText=text.replace(/(^[^@].*)(\r\n|\r|\n)/gm,"$1<BR/>")
#x { font-family: "Courier New", monospace;
white-space: pre; }
<div id="x">@line 1: text text text
line 2:
@line3: text text text
line 4: The line break after this text should be matched
line 5: The line break after this text also should be selected
line 6:
@line 7: this line break should not be selected
</div>
关于javascript - 匹配不以特殊字符开头的行中的换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59752633/