javascript - 匹配不以特殊字符开头的行中的换行符
问题描述
当行以 a 开头时,我想匹配所有\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>
推荐阅读
- java - 从java列表中的每个文件中获取文件名的一部分
- python - 如何进行延迟以便可以多次重播声音
- javascript - 在异步函数中提前返回
- simulink - 参数“增益”的设置无效
- vue.js - 使用组件时Vue分隔符在root中不起作用
- jquery - jQuery Parallax Scroll 在手机/平板电脑上禁用
- linux - SSH 拦截 - Linux
- php - 扁平化嵌套关系按父项排序,然后是子项 - 仅靠 eloquent 可能吗?
- google-app-engine - 如何使用 go 模块在 GAE SE Go 1.11 上导入私有存储库?
- python - 'list' 对象没有属性 'strptime'