Regex 正则表达式否定:处理条件if语句,如果满足该语句,则取消匹配

Regex 正则表达式否定:处理条件if语句,如果满足该语句,则取消匹配,regex,regex-lookarounds,regex-negation,Regex,Regex Lookarounds,Regex Negation,例如,假设我有以下文本: hello world**ant***lorem**cat**antum***************羚羊****兔狗*** 我想匹配只有**和\作为其前面和结尾字符的字符串。所以在上面的例子中,我只想要的匹配是“猫”和“狗”。这意味着如果有额外的周围字符,我必须取消或取消匹配。例如,***dog**或\uuuuuuuuuuuuuu应该失败 我试着用消极的态度来解决这个问题,但是没有用 这是我目前的模式 const pattern = /([^*])\*(\w+)\*(

例如,假设我有以下文本:

hello world**ant***lorem**cat**antum***************羚羊****兔狗***

我想匹配只有
**
\
作为其前面和结尾字符的字符串。所以在上面的例子中,我只想要的匹配是“猫”和“狗”。这意味着如果有额外的周围字符,我必须取消或取消匹配。例如,
***dog**
\uuuuuuuuuuuuuu
应该失败

我试着用消极的态度来解决这个问题,但是没有用

这是我目前的模式

const pattern = /([^*])\*(\w+)\*([^*])/g;
const match = pattern.exec(text);
const annotatedText = match[0];
const matchedText = match[1];

// Return if annotatedText is a possible match for bolditalic
if (annotatedText.startsWith("***") || annotatedText.startsWith("___")) {
        return;
}
// Return if the matchedText has spaces in between
if (/\s/.test(matchedText)) {
        return;
}
if (text.match(/^([*_ \n]+)$/g)) {
        return;
}
在javascript正则表达式中

本质上,我想删除javascript字符串检查,并在正则表达式模式本身上添加逻辑。

使用

/(?
看

解释

--------------------------------------------------------------------------------
(?
JavaScript代码

const string='hello world**ant***lorem**cat**antum************羚羊****兔子***狗***';

console.log(string.match)(/)您好,谢谢您的回答!我已经测试过了,匹配效果很好。一个警告:是否可以捕获后面和后面的字符?例如:
hello world**dog**
应该返回
**dog**
而不是
dog
@wolfgang更简单:
(请参阅。如果它对你有用的话,请考虑接受/投票。完美!谢谢,我将把它作为正确的解决方案。