正则表达式 - 如果模式以 \ 开头则不匹配

Regex - don't match if pattern begins with \

(?:[^\])\*(.*?)\*

这行得通,但是如果字符串以 *something* 开头则它不匹配,所以我需要以某种方式使其在开头不匹配任何内容或除 \

之外的任何内容

用例子编辑:

"text *something*" - matches (correct)
"text \*something*" - doesnt match (correct)
"*something* text" - doesnt match (incorrect)

您可以使用这个正则表达式:

(?:^|[^\])\*(.*?)\*

RegEx Demo

  • (?:^|[^\]) 将匹配输入的开始或除 \
  • 以外的任何内容

如果你的正则表达式支持它,你可以在后面使用负向外观:

(?<!\)\*(.*?)\*