正则表达式匹配JavaScript的括号和括号

时间:2016-05-13 08:45:52

标签: javascript regex

我有这个正则表达式匹配括号内的文字:

/\([^\)]*?\)/g

我希望能够匹配括号和括号,以便检测字符串中的括号和括号,以便为其着色。

这应该是字符串:

The (quick) brown [fox]

我想为(quick)[fox]着色,所以我需要正则表达式来匹配括号和括号。

感谢。

4 个答案:

答案 0 :(得分:7)

这应该有效:

/\([^)]*\)|\[[^\]]*\]/g;

尝试下面的内容:

var str = "The (quick) brown [fox]";

var re = /\([^)]*\)|\[[^\]]*\]/g;

str.match(re).forEach(function(m) {
  document.body.insertAdjacentHTML('beforeend', m + '<br>');
});

<强> Regex101

答案 1 :(得分:0)

就是这样:

\(\w+\)|\[\w+\]

您可以对其进行测试here

答案 2 :(得分:0)

这个正则表达式

/[(\[][^\)\]]*?[)\]]/g

应该为你做。这样就可以混合这样的括号。

The (quick] brown [fox)

See it here at regex101

答案 3 :(得分:0)

正确解析嵌套括号并不容易。
这是我使用的解决方案,它基于Steve Levithan流行的xregexp软件包。
请注意, left right 参数是regx表达式,因此该解决方案也可以处理更困难的情况。有关更多详细信息和选项,请参考http://xregexp.com/api/#matchRecursive

const XRegExp = require('xregexp');

// test: match ${...}
matchRecursive('${a${b}c} d${x} ${e${}${f}g}', '\\${', '}').forEach(match => {
    console.log(match.innerText);
});

/* yields:
a${b}c
x
e${}${f}g
*/

interface XRegExpPart { start: number; end: number; name: string; value: string; }
export interface XRegExpMatch { index: number; outerText: string; innerText: string; left: string; right: string; }

export function replaceRecursive(text: string, left: string, right: string, replacer: (match: XRegExpMatch) => string, flags: string = 'g', escapeChar?: string): string {
    const matches: XRegExpMatch[] = matchRecursive(text, left, right, flags, escapeChar);
    let offset: number = 0;

    for (const match of matches) {
        const replacement = replacer(match);
        if (replacement == match.outerText) continue;
        text = replaceAt(text, match.index + offset, match.outerText.length, replacement);
        offset += replacement.length - match.outerText.length;
    }
    return text;
}

export function matchRecursive(text: string, left: string, right: string, flags: string = 'g', escapeChar?: string): XRegExpMatch[] {
    // see: https://github.com/slevithan/xregexp#xregexpmatchrecursive
    // see: http://xregexp.com/api/#matchRecursive
    const parts: XRegExpPart[] = XRegExp.matchRecursive(text, left, right, flags, { valueNames: [null, 'left', 'match', 'right'], escapeChar: escapeChar });
    const matches: XRegExpMatch[] = [];
    let leftPart: XRegExpPart;
    let matchPart: XRegExpPart;

    for (const part of parts) {
        // note: assumption is made that left, match and right parts occur in this sequence
        switch (part.name) {
            case 'left':
                leftPart = part;
                break;
            case 'match':
                matchPart = part;
                break;
            case 'right':
                matches.push({ index: leftPart!.start, innerText: matchPart!.value, outerText: leftPart!.value + matchPart!.value + part.value, left: leftPart!.value, right: part.value });
                break;
            default:
                throw new Error(`Unexpected part name: '${part.name}'.`);
        }
    }

    return matches;
}

export function replaceAt(string: string, index: number, length: number, replacement: string): string {
    return string.substr(0, index) + replacement + string.substr(index + length);
}