【问题标题】:Regex Negation: Handling conditional if statements that cancel the match if fulfilled正则表达式否定:处理条件 if 语句,如果满足则取消匹配
【发布时间】:2021-01-26 23:48:06
【问题描述】:

比如说我有这样的文字:

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

我想匹配只有 ** 和 __ 作为其前面和结束字符的字符串。所以在上面的例子中,我只想匹配“cat”和“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 字符串检查并在正则表达式模式本身上添加逻辑。

【问题讨论】:

    标签: regex regex-lookarounds regex-negation


    【解决方案1】:

    使用

    /(?<=(?<!\*)\*\*)\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))

    【讨论】:

    • 您好,感谢您的回答!我已经对其进行了测试,并且匹配效果很好。一个警告:是否有可能同时捕捉到后面和后面的字符?例如:hello world **dog** 应该返回 **dog** 而不是 dog
    • @wolfgang 更简单:(?&lt;!\*)\*\*\w+\*\*(?!\*)|(?&lt;!_)__\w+__(?!_)。见proof。如果对您有用,请考虑接受/支持。
    • 完美!非常感谢,我会将此设置为正确的解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-13
    • 2020-08-26
    • 2022-07-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多