javascript正则是将_xxx_转换为[i] xxx [i]忽略<>> (URL)或``(代码)
我正在尝试重新格式化稀疏格式以供bbcode,并需要一些帮助。 Slack确实是这样的:
_this is italic_ and this isn't
我的当前表达式(/\ _([[^\ _]*)\ _/gm
)有效,但不幸的是,在URL和内部代码snippets中拾取了下划线。 Slack格式的URL和类似的代码:
<www.thislink.com|here's a link>
`here's a code snippet`
如何告诉Regex不要在链接或代码段中匹配任何下划线对?我一直在尝试负面的lookahead和beebhind,但没有成功。
I'm trying to reformat Slack formatting to bbcode and need a little help. Slack does italics like this:
_this is italic_ and this isn't
My current expression (/\_([^\_]*)\_/gm
) works but unfortunately picks up underscores in URLs and inside code snippets. Slack formats URLs and code like this:
<www.thislink.com|here's a link>
`here's a code snippet`
How can I tell regex not to match any underscore pairs inside a link or code snippet? I've been trying negative lookahead and lookbehind but without success.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要匹配并捕获所需的内容,并匹配您不需要的东西。
一旦获得匹配,请分析并实现适当的代码逻辑:
请参阅 the Regex demo 。 详细信息:
&lt; [^&lt;&gt; |]*(?:\ | [^&lt;&gt;]*)*)? ;
,然后除了&lt;
,&gt;
和|
之外的零或更多字符,然后是| ,然后除
&lt;
和&gt;
外,然后零或更多字符,然后a&gt;
char|
- 或`[^`]*
- 回压,零或更多的chars,除了背景和回程|
| - 或_(( [^_]*)_
-_
,第1组:零或更多字符以外的其他字符,_
,a_
。You need to match and capture what you need and just match what you do not need.
Once you get a match, analyze it and implement the appropriate code logic:
See the regex demo. Details:
<[^<>|]*(?:\|[^<>]*)?>
- a<
, then zero or more chars other than<
,>
and|
, then an optional sequence of a|
and then zero or more chars other than<
and>
and then a>
char|
- or`[^`]*`
- a backtick, zero or more chars other than a backtick and a backtick|
- or_([^_]*)_
-_
, Group 1: zero or more chars other than_
, a_
.