简体   繁体   English

正则表达式匹配大括号之间的任何内容,但引用时除外

[英]Regex to match anything between braces except when quoted

Does anyone know the best approach with regex to find anything in a string between braces, except when quoted.有谁知道使用正则表达式在大括号之间的字符串中查找任何内容的最佳方法,除非被引用。 Example:例子:

This is my {string between braces} which the pattern should match, but this "{string between braces}" should be ignored.这是我的{string between braces}该模式应该匹配,但这种"{string between braces}"应该被忽略。

This regex will get me anything between braces...这个正则表达式会让我在大括号之间得到任何东西......

/\{([^}]+)\}/g

Edit:编辑:

For those who have provided potential solutions so far, i thank you.对于那些迄今为止提供了潜在解决方案的人,我感谢你们。 To those who have questioned the ambiguity of this, apologies, it was quickly fired off with the intention of coming back to edit.对于那些质疑这一点含糊不清的人,抱歉,它很快就被解雇了,打算回来编辑。

Given the following scenarios:鉴于以下场景:

var str1 = 'Lorem ipsum dolor {sit} amet, consectetur {adipiscing} elit, 
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua';

Expect Result: {sit} and {adipiscing} be matched预期结果:{sit} 和 {adipiscing} 匹配

var str2 = 'Lorem "ipsum "dolor {sit} amet", consectetur" {adipiscing} elit, 
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua';

Expected Result: {sit} and {adipiscing} be matched (since neither are between open quotes)预期结果:{sit} 和 {adipiscing} 匹配(因为两者都不在双引号之间)

var str3 = 'Lorem ipsum "dolor {sit} amet", consectetur {adipiscing} elit, 
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua';

Expected Result: {adipiscing}预期结果:{adipiscing}

For the purpose of clarity, in terms quotes, we are talking quotes only, not apostrophes.为了清楚起见,在引号方面,我们只谈论引号,而不是撇号。

One option is to match all the strings that start with "{...}" and capture the alternative using an alternation in a group一种选择是,以匹配所有以启动字符串"{...}"使用,并捕获替代交替组中

"{[^{}]*}"|\{([^}]+)\}

Regex demo正则表达式演示

 const regex = /"{[^{}]*}"|\\{([^}]+)\\}/g; const str = `{string between braces} "{string between braces 2}" `; let m; while ((m = regex.exec(str)) !== null) { // This is necessary to avoid infinite loops with zero-width matches if (m.index === regex.lastIndex) { regex.lastIndex++; } if (m[1]) { console.log(m[1]); } }

Another option is to also match either a " at the start or either a " at the end to not capture the group when having a single double quote.另一种选择是也匹配或者一个"在开始或任一"在末端具有单一双引号时不捕获该组。

"{[^{}]+}"?|{[^{}]+}"|{([^{}]+)}

Regex demo正则表达式演示

In parts在零件中

  • "{[^{}]+}"? Match from opening "{ till closing } and optional "匹配从打开"{直到关闭}和可选"
  • | Or或者
  • {[^{}]+}" Match from opening { till closing } and " {[^{}]+}"匹配从开始{到结束}"
  • | Or或者
  • { Match opening { {比赛开场{
  • ([^{}]+) Capture group 1, match 1+ times any char except { or } ([^{}]+)捕获第 1 组,匹配除{}之外的任何字符 1+ 次
  • } Match closing } }比赛结束}

 const regex = /"{[^{}]+}"?|{[^{}]+}"|{([^{}]+)}/g; const strings = [ '{string between braces}', '"{string between braces 2}"', '"{string between braces 3}', '{string between braces 4}"', 'start"{str5}"{str6}"end' ].forEach(s => { let m; while ((m = regex.exec(s)) !== null) { // This is necessary to avoid infinite loops with zero-width matches if (m.index === regex.lastIndex) { regex.lastIndex++; } if (m[1]) { console.log(m[1]); } } });

Maybe,也许,

{([^}]*)}(?!")

might be close.可能很接近。

 const regex = /{([^}]*)}(?!")/gm; const str = `This is my {string between braces} which the pattern should match, but this "{string between braces}" should be ignored`; let m; while ((m = regex.exec(str)) !== null) { // This is necessary to avoid infinite loops with zero-width matches if (m.index === regex.lastIndex) { regex.lastIndex++; } m.forEach((match, groupIndex) => { console.log(`Found match, group ${groupIndex}: ${match}`); }); }


If you wish to simplify/update/explore the expression, it's been explained on the top right panel of regex101.com .如果你想简化/更新/探索表达式,它已在regex101.com 的右上角面板中进行了解释 You can watch the matching steps or modify them in this debugger link , if you'd be interested.如果您有兴趣,可以在此调试器链接中观看匹配步骤或修改它们。 The debugger demonstrates that how a RegEx engine might step by step consume some sample input strings and would perform the matching process.调试器演示了 RegEx 引擎如何逐步使用一些示例输入字符串并执行匹配过程。


RegEx Circuit正则表达式电路

jex.im visualizes regular expressions: jex.im可视化正则表达式: 在此处输入图片说明


We can possibly look at some other boundaries too:我们也可以看看其他一些边界:

(?:\s|^){([^}]*)}(?:\s|$)

RegEx Demo正则表达式演示

I would just match the part with the quotes first, and keep the part in the braces in a capture group.我只会先将部分与引号匹配,然后将部分保留在捕获组中的大括号中。

"[^"]*"|\{([^}]*)\}

regex101 demo regex101 演示

 const re = /"[^"]*"|\\{([^}]*)\\}/g; const str = `there is a {braced part} that's a "quoted {braced part}"`; let match; while ((match = re.exec(str)) !== null) { if (match.index === re.lastIndex) { re.lastIndex++; } if (match[1]) { console.log(match[1]); } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM