【发布时间】:2021-05-08 10:17:20
【问题描述】:
我正在使用这个正则表达式字符串来查找所有句号、感叹号和问号,它们周围有空格或不是小数的一部分:
/\.(?=\s|$)|\?(?=\s|$)|\!(?=\s|$)/g
我正在使用 mark.js 来突出显示这个 RegEx 字符串。我怎样才能修改这个字符串(或使用另一个字符串),它不会突出显示紧跟在括号之后的问号,或者[?
这是我的代码:
function Highlight() {
var instance = new Mark(document.getElementById("example"));
instance.unmark();
instance.markRegExp(/\.(?=\s|$)|\?(?=\s|$)|\!(?=\s|$)/g);
}
window.onload = Highlight();
mark {
background: pink;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/mark.js/8.11.1/mark.min.js"></script>
<div id="example">
<p>This is an example paragraph. I would like all the periods to be highlighted. Woohoo! This works very well! Yay! Alright, onto question marks. This is demo2. [? flagged ?] 5.5 is a number. 0.3374 is another number. Does this work?</p>
</div>
Mark.js 也有一个 unmark() 方法来取消标记,但我不知道如何将 RegEx 与 unmark() 一起使用。非常感谢您的帮助。
【问题讨论】:
-
在
?后面使用否定的lookbehind
标签: javascript regex highlight mark.js