【发布时间】:2018-07-02 07:39:27
【问题描述】:
我有以下字符串,我希望用\n 字符替换前两次出现的<br>。
<br><br><br>. Do not replace <br> here.
1. One
2. Two
3. Three<br><br><br>End of List. Replace first two <br> with \n
New line follows.
<br><br><br>. Do not replace <br> here.
我确实写了我的正则表达式here。我对正则表达式很陌生,我确信这不是一个优化的解决方案。
经过一些尝试,我能够选择<br><br> 作为捕获组。
我希望这第三个捕获组成为我选择的匹配项,因此我可以轻松地将其替换为 \n。
有人可以帮我弄这个吗?
我的预期输出是:
<br><br><br>. Do not replace <br> here.
1. One
2. Two
3. Three\n<br>End of List. Replace first two <br> with \n
New line follows.
<br><br><br>. Do not replace <br> here.
var str = `1. One
2. Two
3. Three<br><br><br>End of List
New line follows
`
console.log(
str.match(/[\n\r].*(\d\.\s+)(?!.*[\n\r](\d\.\s+)).*((<br\s*\/?>){2})/)
);
【问题讨论】:
-
我给你做了一个sn-p。预期的输出究竟是什么?
-
目前还不清楚你想要做什么:
str.replace('<br><br>', '\n') -
谢谢你们的回复。我已经编辑了我的问题,以向您展示我的预期输出。
-
@Mark_M 我正在尝试检测
是否位于列表末尾。如果我发现这种模式,我希望它被换行符替换。
标签: javascript regex