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

Regex Negation: Handling conditional if statements that cancel the match if fulfilled

例如我有这段文字:

hello world **ant*** lorem **cat** opposum** *** ***antelope*** *rabbit __dog__

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

我试过用消极的眼光来解决这个问题 http://www.regular-expressions.info/lookaround.html 但无济于事。

这是我目前的模式

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 字符串检查并在正则表达式模式本身上添加逻辑。

使用

/(?<=(?<!\*)\*\*)\w+(?=\*\*(?!\*))|(?<=(?<!_)__)\w+(?=__(?!_))/gi

proof

说明

--------------------------------------------------------------------------------
  (?<=                     look behind to see if there is:
--------------------------------------------------------------------------------
    (?<!                     look behind to see if there is not:
--------------------------------------------------------------------------------
      \*                       '*'
--------------------------------------------------------------------------------
    )                        end of look-behind
--------------------------------------------------------------------------------
    \*                       '*'
--------------------------------------------------------------------------------
    \*                       '*'
--------------------------------------------------------------------------------
  )                        end of look-behind
--------------------------------------------------------------------------------
  \w+                      word characters (a-z, A-Z, 0-9, _) (1 or
                           more times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  (?=                      look ahead to see if there is:
--------------------------------------------------------------------------------
    \*                       '*'
--------------------------------------------------------------------------------
    \*                       '*'
--------------------------------------------------------------------------------
    (?!                      look ahead to see if there is not:
--------------------------------------------------------------------------------
      \*                       '*'
--------------------------------------------------------------------------------
    )                        end of look-ahead
--------------------------------------------------------------------------------
  )                        end of look-ahead
--------------------------------------------------------------------------------
 |                        OR
--------------------------------------------------------------------------------
  (?<=                     look behind to see if there is:
--------------------------------------------------------------------------------
    (?<!                     look behind to see if there is not:
--------------------------------------------------------------------------------
      _                        '_'
--------------------------------------------------------------------------------
    )                        end of look-behind
--------------------------------------------------------------------------------
    __                       '__'
--------------------------------------------------------------------------------
  )                        end of look-behind
--------------------------------------------------------------------------------
  \w+                      word characters (a-z, A-Z, 0-9, _) (1 or
                           more times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  (?=                      look ahead to see if there is:
--------------------------------------------------------------------------------
    __                       '__'
--------------------------------------------------------------------------------
    (?!                      look ahead to see if there is not:
--------------------------------------------------------------------------------
      _                        '_'
--------------------------------------------------------------------------------
    )                        end of look-ahead
--------------------------------------------------------------------------------
  )                        end of look-ahead

JavaScript代码:

const string = 'hello world **ant*** lorem **cat** opposum** *** ***antelope*** *rabbit __dog__';
console.log(string.match(/(?<=(?<!\*)\*\*)\w+(?=\*\*(?!\*))|(?<=(?<!_)__)\w+(?=__(?!_))/gi))