【问题标题】:what does this mean? what string will be matched using the expression?这是什么意思?使用表达式将匹配什么字符串?
【发布时间】:2012-07-22 20:22:36
【问题描述】:

我检查了嘶嘶声的代码并看到了一个定义。

var chunker = /((?:\((?:\([^()]+\)|[^()]+)+\)|\[(?:\[[^\[\]]*\]|['"][^'"]*['"]|[^\[\]'"]+)+\]|\\.|[^ >+~,(\[\\]+)+|[>+~])(\s*,\s*)?((?:.|\r|\n)*)/g,

我想知道如何找出这个正则表达式将匹配哪些字符串?

【问题讨论】:

  • 第一次看到正则表达式的反向问题。
  • 为什么反对票和接近票?如果这件作品实际上是站在那里没有评论,那么这是一个有效的问题
  • 我什至不相信这个正则表达式是有效的(看起来它是一个括号配对关闭)
  • @BradChristie:在 JS 中有效。

标签: regex sizzle


【解决方案1】:

要学习如何手工完成,您可以像这样简单地分解它(这是一项将来肯定会帮助您的任务):

var chunker = /((?:\((?:\([^()]+\)|[^()]+)+\)|\[(?:\[[^\[\]]*\]|['"][^'"]*['"]|[^\[\]'"]+)+\]|\\.|[^ >+~,(\[\\]+)+|[>+~])(\s*,\s*)?((?:.|\r|\n)*)/g,

第一部分是了解一个正则表达式通常是如何编写的,所以在这种情况下应该是:

var variableName = /stuff/g;
                   ^  ^  ^^
        delimiters/---+--/|
                      |   |
                regex /   \global modifier, can also have a case modifier

所以让我们去掉修饰符以及开始和结束,只计算正则表达式:

((?:\((?:\([^()]+\)|[^()]+)+\)|\[(?:\[[^\[\]]*\]|['"][^'"]*['"]|[^\[\]'"]+)+\]|\\.|[^ >+~,(\[\\]+)+|[>+~])(\s*,\s*)?((?:.|\r|\n)*)

仍然是 gobbledygook,但让我们继续:正则表达式用于捕获内容,我们知道未转义(意味着他们在被转义的字符之前使用反斜杠 \)括号捕获大括号,让我们添加每个括号周围都有一些换行符(仅用于检查!!!)和管道表示“或”

(
  (?:
    \(
      (?:
          \(
              [^()]+
          \)
        |
          [^()]+
      )+
    \)
|
  \[
    (?:
        \[[^\[\]]*\]
      |
        ['"][^'"]*['"]
      |
        [^\[\]'"]+
    )+
  \]
|
  \\.
|
  [^ >+~,
  (
      \[\\]+)+
    |
      [>+~]
  )
  (\s*,\s*)?
  (
    (?:
        .
      |
        \r
      |
        \n
    )*
  )

方括号的意思是“有选择地将其中的内容作为选择集中的任何值匹配”,所以现在我要添加一些 cmets,然后让您计算其余部分(而且,它看起来确实像缺少一些括号)

(
  (?:                 //<-- start a non-capturing group (means, don't tell the app we matched)
    \(
      (?:             //<-- start a non-capturing group (means, don't tell the app we matched)
          \(
              [^()]+  //one or more paren pairs inside a paren pair
          \)
        |
          [^()]+      //or one or more paren pairs
      )+
    \)
|
  \[
    (?:
        \[[^\[\]]*\]
      |
        ['"][^'"]*['"]
      |
        [^\[\]'"]+
    )+
  \]
|
  \\.
|
  [^ >+~,
  (
      \[\\]+)+
    |
      [>+~] //either a tilde, plus or opening brace
  )
  (\s*,\s*)?
  (
    (?:
        .
      |
        \r
      |
        \n
    )*
  )

【讨论】:

    【解决方案2】:

    见this article。多行正则表达式中的解释:

    var chunker = /
     (
      (?:
       # One or more sets of parentheses that contain a string, or another set of 
       parentheses with a string
       \(
       (?:
        \([^()]+\)
        |
        [^()]+
       )+
       \)
       |
       # Or one or more sets of brackets that contain a string, or another set of
       brackets with a string
       \[
       (?:
        \[[^\[\]]*\]
        |
        ['"][^'"]*['"]
        |
        [^\[\]'"]+
       )+
       \]
       |
       # Or a backslash followed by any character
       \\.
       |
       # Or one or more of any except these characters: > +~,([\
       [^ >+~,(\[\\]+
      )+
      # or any one of these characters: >+~
      |
      [>+~]
     )
     # followed by zero or one commas, which may be surrounded by whitespace
     (\s*,\s*)?
     # followed by zero or more of anything, including line endings
     ((?:.|\r|\n)*)
    /g
    

    此表达式包含三个匹配组:“已验证”选择器表达式、最终逗号以及之后的所有内容。它将在选择器上不断被调用以将其拆分为多个部分,有关详细信息,请参阅Sizzle 构造函数。

    【讨论】:

      【解决方案3】:

      这将是推测性的。但是,使用 RegexBuddy 您可以“记录”并可视化表达式。

      因为它是一个付费应用程序,所以我在pastebin 上导出了 cmets。希望这会帮助你。 (请注意,它不支持 Sizzle,并且我使用 JavaScript 语言来记录表达式)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-05-01
        • 2010-10-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-04
        • 2020-10-23
        相关资源
        最近更新 更多