【问题标题】:javascript regex, word not followed and not preceded by specific charjavascript正则表达式,单词不跟随且不以特定字符开头
【发布时间】:2012-12-24 08:37:20
【问题描述】:

我想我不是很擅长正则表达式,所以这是我的问题(我确信它很容易解决,但不知何故我似乎找不到方法)

var word = "aaa";
text = text.replace( new RegExp(word, "g"),
                     "<span style='background-color: yellow'>"+word+"</span>");

这在大多数情况下都有效。

我想要做的是仅当单词后面没有字符 / 并且前面没有字符 " 时,正则表达式才起作用。

【问题讨论】:

    标签: javascript regex


    【解决方案1】:

    您将要使用负前瞻。

    正则表达式:'[^"]' + word + '(?!/)'

    编辑:虽然看起来你已经通过避免后视找到了答案,但这并不重要,但 Rohit 注意到了一些我没有注意到的事情。您将需要捕获 [^\"] 并将其包含在替换中,这样它就不会被丢弃。 这对于查看头来说不是必需的,因为根据定义,环视不包括在捕获中。

    【讨论】:

    • 非常感谢。所以如果我也希望它不以 char " 开头,我应该使它成为 "(?!\")" + word + "(?!\/)" ?
    • 如果你想确保 " 不在单词之前,你需要使用否定的look-behind。所以你需要(?&lt;!\") + word + (?!\/)。看看这个链接,这是我学习如何使用的地方这些:regular-expressions.info/lookaround.html
    • Javascript 不支持后视。
    • new RegExp("(?
    • '[^"]' + word + '(?!/)' 就够了。
    【解决方案2】:

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

    '([^"])' + word + '(?!/)'
    

    并将其替换为 - "$1g"

    请注意,Javascript 不支持后视。所以,你需要捕获前一个字符,并确保它不是",使用否定字符类。

    http://fiddle.re/zdjt查看演示

    【讨论】:

    • 如果匹配在行首或行尾,则失败。也许这个问题可以提到这是否重要。
    【解决方案3】:

    首先,问题可以理解为

    1. 匹配词不在前面AND不在后面"aaaaaa/ 应该匹配,与 "aaa/ 相同)和 as
    2. 匹配单词前面没有后面没有有一些字符("aaaaaa/ 不应该匹配,而 "aaa/ 应该匹配)。

    如果您使用的是 支持 ECMAScript 2018 的 JavaScript 环境,您可以使用后视解决方案:

    const word = 'aaa',                     // Word to find
        left = '"',                         // Left-hand negative marker
        right = '/',                        // Right-hand negative marker
        text = 'aaa aaa/ "aaa "aaa/ aaaaa'; // Test string
    
    // Relace any 'word' that does not start with " OR does not end with /
    var regex = new RegExp(String.raw`\b(?<!${left})${word}\b(?!${right})`, 'g')
    console.log( text.replace(regex, "<span style='background-color: yellow'>$&</span>") );
    // => <span style='background-color: yellow'>aaa</span> aaa/ "aaa "aaa/ aaaaa
    
    // Replace any 'word' that does not start with " AND does not end with /
    var regex = new RegExp(String.raw`\b${word}\b(?!(?<=${left}${word})${right})`, 'g')
    console.log( text.replace(regex, "<span style='background-color: yellow'>$&</span>") );
    // => <span style='background-color: yellow'>aaa</span> <span style='background-color: yellow'>aaa</span>/ "<span style='background-color: yellow'>aaa</span> "aaa/ aaaaa

    如果你使用的 JavaScript 环境不支持ECMAScript 2018,你可以使用

    var word = 'aaa',                       // Word to find
        left = '"',                         // Left-hand negative marker
        right = '/',                        // Right-hand negative marker
        text = 'aaa aaa/ "aaa "aaa/ aaaaa'; // Test string
    
    // Relace any 'word' that does not start with " OR does not end with /, one match expected
    var regex = new RegExp("([^" + left + "\\w]|^)(" + word + ")\\b(?!" + right + ")", 'g')
    console.log( text.replace(regex, "$1<span style='background-color: yellow'>$2</span>") );
    // => <span style='background-color: yellow'>aaa</span> aaa/ "aaa "aaa/ aaaaa
    
    // Same as above, but in case "left" is a sequence of chars, not a single char, or a longer pattern:
    var regex = new RegExp("(" + left + ")?\\b(" + word + ")\\b(?!" + right + ")", 'g')
    console.log( text.replace(regex, function(x,g,gg) { return g ? x : "<span style='background-color: yellow'>" + gg + "</span>";}) );
    // => <span style='background-color: yellow'>aaa</span> aaa/ "aaa "aaa/ aaaaa
    
    // Replace any 'word' that does not start with " AND does not end with /, three matches expected
    var regex = new RegExp("(" + left + word + right + ")|\\b" + word + "\\b", 'g')
    console.log( text.replace(regex, function(x, g) { return g || "<span style='background-color: yellow'>" + x + "</span>";}) );
    // => <span style='background-color: yellow'>aaa</span> <span style='background-color: yellow'>aaa</span>/ "<span style='background-color: yellow'>aaa</span> "aaa/ aaaaa

    注意:

    • 我在这里通过\b、单词边界(以及一种解决方法中的否定字符类中的\w 构造)使用全字匹配
    • 如果rightleft 字符/字符串包含特殊的正则表达式元字符,请注意escape them before using as part of a regex
    • word 变量可能会出现同样的转义需求,但在这种情况下,如果 word 以特殊的正则表达式元字符开头或/和结尾,单词边界 (\b) 将不再起作用,您将需要使用adaptive/dynamic word boundaries

    【讨论】:

      猜你喜欢
      • 2015-06-24
      • 1970-01-01
      • 2020-06-03
      • 1970-01-01
      • 1970-01-01
      • 2010-11-17
      • 1970-01-01
      • 2023-03-15
      • 1970-01-01
      相关资源
      最近更新 更多