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

标签 javascript regex regex-lookarounds

我想匹配所有 \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

https://regexr.com/4s98n

注意我不选择文本,我替换\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/

相关文章:

python - 有没有办法通过Python中的正则表达式找到子字符串索引?

javascript - 将 String html 转换为数组对象类型

python正则表达式向前看正+负

regex - 使用 C++ RegEx 进行后视的替代方法是什么?

php - Ajax 搜索引擎优化技术

javascript - 增强 Vanilla JS 功能

javascript - jQuery - 滚动功能不断触发 : Only Once Please

regex - 为什么 "ab(cd|c)*d"完全匹配 "abcdcdd"但 "ab(c|cd)*d"不匹配?尽管他们彼此相似

java - 正则表达式在捕获之前仅与两个相似的 URL 进行非常具体的匹配

javascript - 使 SVG 变换矩阵围绕其中心旋转