正则表达式调整:删除括号
我有以下脚本:
$(".Text").contents().each(function () {
$(this).replaceWith($(this).text()
.replace(/\[([^\]]*)\]/g, '<span class="IT_Symbol" style="display:inline;border: 1px solid blue;">$&</span>')
);
});
它找到方括号之间的任何内容并将其用类包装。元素样式是这样我可以看到它的工作原理,因为该脚本是通过双击触发的。目前它可以找到方括号之间的所有内容,包括方括号本身。如果可能的话,我想去掉括号,但保留中间的内容。
I have the following script:
$(".Text").contents().each(function () {
$(this).replaceWith($(this).text()
.replace(/\[([^\]]*)\]/g, '<span class="IT_Symbol" style="display:inline;border: 1px solid blue;">amp;</span>')
);
});
It finds anything between square brackets and wraps it with a class. The element style is so i can see it working, as this script is triggered by a doubleclick. Currently it finds everything between square brackets including the brackets themselves. If possible, i'd like to remove the brackets, but keep what's between.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您将替换为
$&
,这是整个匹配的文本。如果替换为$1
,则仅匹配第一组,即([^\]]*)
,因此排除周围的括号。You're replacing with
$&
, which is the whole matched text. If you replace with$1
instead, this only matches the first group, which is([^\]]*)
, and therefore excludes the surrounding brackets.