这是什么意思?使用表达式匹配什么字符串?

时间:2012-07-24 13:42:52

标签: regex sizzle

我查看了sizzle代码并查看了定义。

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

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

3 个答案:

答案 0 :(得分:4)

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

此表达式包含三个匹配组:A"已验证"选择器表达式,最终逗号以及之后的所有内容。它将在选择器上连续调用以将其分成几部分,有关详细信息,请参阅Sizzle构造函数。

答案 1 :(得分:1)

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

Documentation using RegexBuddy

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

答案 2 :(得分:0)

要了解如何手动完成,您可以简单地将其分解(这是将来一定会帮助您的任务):

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
    )*
  )

方括号意味着“选择性地匹配其中的内容与选择集中的任何值”,所以现在我要添加一些注释,让你解决其余的问题(同样,它确实看起来像一些parens失踪了)

(
  (?:                 //<-- 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
    )*
  )
相关问题