首页 > 解决方案 > Regex for something not between quotes and parenthesis

问题描述

I want to match the word hello that is not between any type of quote ('' or "" or ``) and not in parentheses.

An example match would be:

'hello'(hello)('hello')hello
                       ^^^^^ # the last hello is matched

If there is any way to do something like this I would appreciate it. Thanks!

What i have tried is this:

'(hello)+(?=(?:[^\'"`]*[\'"`][^\'"`]*[\'"`])*[^\'"`]*$)(?!\()(?<!\))'

but it always returns nothing as a match

I am trying to get the index of the match.

标签: javascriptregex

解决方案


正则表达式并不是真正的正确工具。此外,JavaScript(目前)不支持(?R)-recursion 逻辑,因此为了支持嵌套括号检测,最好使用普通的旧for循环。

这是一个完成这项工作的函数。它使用您提供的示例输入和单词运行,并返回找到良好匹配的索引——在本例中为 23:

function findOccurrence(input, word) {
    for (let i = 0, j = -1, nesting = 0; i < input.length; i++) {
        let ch = input[i];
        if (i > j) {
            j = input.indexOf(word, i); // find next occurrence of word
            if (j < 0) return -1; // no more occurrences...
        }
        if (i === j && nesting === 0) return i; // bingo!
        if ("`'\"".includes(ch)) { // opening quote
            i = input.indexOf(ch, i+1); // jump to closing quote (of same type)
        } else if (ch === "(") {
            nesting++;
        } else if (nesting > 0 && ch === ")") {
            nesting--;
        }
    }
    return -1; // not found
}

// example:
console.log(findOccurrence("'hello'(hello)('hello')hello", "hello"));


推荐阅读