简体   繁体   English

涉及花括号的正则表达式不起作用,而对于方括号则有效

[英]Regex involving curly braces not working, while for square brackets it does

I have this regex that matches something like [abc] as a string pattern, with the help of我有这个正则表达式匹配类似[abc]作为字符串模式,在
"[]": "((\\[[^\\]]*($|\\]))(\\][^\\]]*($|\\]))*)

So it will catch [ABC] from ABC AB [ABC] BC .所以它会从ABC AB [ABC] BC中捕获[ABC] So anything enclosed in a square bracket.所以任何东西都包含在方括号中。

It works as it's supposed to.它按预期工作。

Then I wrote an expression like然后我写了一个表达式
"{{}}": "((\\{\\{[^\\}\\}]*($|\\}\\}))(\\}\\}[^\\}\\}]*($|\\}\\}))*)"

to catch something like {{abc}} .捕捉像{{abc}}这样的东西。 Now, this does work in online regex testers, it catches {{abc}} from ABC AB {{ABC}} BC .现在,这确实适用于在线正则表达式测试器,它从ABC AB {{ABC}} BC捕获{{abc}}

But it's not doing anything when I have it in a JS code.但是当我在 JS 代码中使用它时,它什么也没做。 While, the square bracket expression does what it's supposed to.同时,方括号表达式做了它应该做的事情。 Am I missing something?我错过了什么吗?

  createStringRegex(stringTypes) {
    return new RegExp('^(' + this.createStringPattern(stringTypes) + ')', 'u');
  }

  // This enables the following string patterns:
  // 1. backtick quoted string using `` to escape
  // 2. square bracket quoted string (SQL Server) using ]] to escape
  // 3. double quoted string using "" or \" to escape


  createStringPattern(stringTypes) {
    const patterns = {
      '``': '((`[^`]*($|`))+)',
      '[]': '((\\[[^\\]]*($|\\]))(\\][^\\]]*($|\\]))*)',
      "{{}}": "((\\{\\{[^\\}\\}]*($|\\}\\}))(\\}\\}[^\\}\\}]*($|\\}\\}))*)",
      '""': '(("[^"\\\\]*(?:\\\\.[^"\\\\]*)*("|$))+)',
      "''": "(('[^'\\\\]*(?:\\\\.[^'\\\\]*)*('|$))+)",
      "N''": "((N'[^N'\\\\]*(?:\\\\.[^N'\\\\]*)*('|$))+)"
    };

    return stringTypes.map(t => patterns[t]).join('|');
  }

This is the whole snippet surrounding this这是围绕这个的整个片段

I don't see the problem.我没有看到问题。 I also tried to use your code and it seems to work:我也尝试使用你的代码,它似乎工作:

 function createStringRegex(stringTypes) { return new RegExp('^(' + this.createStringPattern(stringTypes) + ')', 'u'); } // This enables the following string patterns: // 1. backtick quoted string using `` to escape // 2. square bracket quoted string (SQL Server) using ]] to escape // 3. double quoted string using "" or \" to escape function createStringPattern(stringTypes) { const patterns = { '``': '((`[^`]*($|`))+)', '[]': '((\\[[^\\]]*($|\\]))(\\][^\\]]*($|\\]))*)', "{{}}": "((\\{\\{[^\\}\\}]*($|\\}\\}))(\\}\\}[^\\}\\}]*($|\\}\\}))*)", '""': '(("[^"\\\\]*(?:\\\\.[^"\\\\]*)*("|$))+)', "''": "(('[^'\\\\]*(?:\\\\.[^'\\\\]*)*('|$))+)", "N''": "((N'[^N'\\\\]*(?:\\\\.[^N'\\\\]*)*('|$))+)" }; return stringTypes.map(t => patterns[t]).join('|'); } console.log("[abc]".match(createStringRegex(["[]"]))); // it matches console.log("{{abc}}".match(createStringRegex(["{{}}"]))); // it matches console.log("[abc]".match(createStringRegex(["{{}}"]))); // it doesn't match

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

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