匹配来自*ABC*的"ABC",但不是来自**ABC**

Lin*_*der 1 regex

我想匹配ABC*ABC*,而不是从**ABC**.

我到目前为止这个;

/[^\*]{1}\*([^\*]+)\*[^\*]+/
Run Code Online (Sandbox Code Playgroud)

要匹配的字符串可以是以下任何一种.

ABC **Don't match this** DEF
ABC *Match this* DEF
*Match this*
**Don't match this**

aio*_*obe 5

你可以使用负面的后视,负面的前瞻.那是

(?<!\*)\*ABC\*(?!\*)
Run Code Online (Sandbox Code Playgroud)

其中读出的是

*ABC*不是先于*,而不是成功*.

  • 或者如果味道不支持lookbehinds(如JavaScript),那么在开头添加它也可能就足够了:`(?!.*\*{2})`. (2认同)