有人可以向我解释这个RegEx吗?

时间:2012-03-17 01:58:14

标签: javascript regex

我看到这行代码和正则表达式让我感到恐慌......

quickExpr = /^(?:[^#<]*(<[\w\W]+>)[^>]*$|#([\w\-]*)$)/

有人可以一点一点地解释 它的作用吗? 感谢,G

2 个答案:

答案 0 :(得分:2)

^(?:[^#<]*(<[\w\W]+>)[^>]*$|#([\w\-]*)$)

Assert position at the start of the string «^»
Match the regular expression below «(?:[^#<]*(<[\w\W]+>)[^>]*$|#([\w\-]*)$)»
   Match either the regular expression below (attempting the next alternative only if this one fails) «[^#<]*(<[\w\W]+>)[^>]*$»
      Match a single character NOT present in the list "#<" «[^#<]*»
         Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
      Match the regular expression below and capture its match into backreference number 1 «(<[\w\W]+>)»
         Match the character "<" literally «<»
         Match a single character present in the list below «[\w\W]+»
            Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
            Match a single character that is a "word character" (letters, digits, etc.) «\w»
            Match a single character that is a "non-word character" «\W»
         Match the character ">" literally «>»
      Match any character that is not a ">" «[^>]*»
         Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
      Assert position at the end of the string (or before the line break at the end of the string, if any) «$»
   Or match regular expression number 2 below (the entire group fails if this one fails to match) «#([\w\-]*)$»
      Match the character "#" literally «#»
      Match the regular expression below and capture its match into backreference number 2 «([\w\-]*)»
         Match a single character present in the list below «[\w\-]*»
            Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
            Match a single character that is a "word character" (letters, digits, etc.) «\w»
            A - character «\-»
      Assert position at the end of the string (or before the line break at the end of the string, if any) «$»


Created with RegexBuddy

答案 1 :(得分:2)

以下是我可以提取的内容:

  • ^字符串的开头。
  • (?:不匹配的群组。
  • [^#<]*任意数量的非#<的连续字符。
  • (<[\w\W]+>)匹配<anything_goes_here>等字符串的小组。
  • [^>]*序列中任意数量的字符不是>

|之后的部分表示第二个正则表达式,如果第一个失败则尝试。那个是#([\w\-]*)

  • ##字符匹配。不那么复杂。
  • ([\w\-]*)是一个匹配任意数量的单词字符或短划线的组。基本上是Things-of-this-form
  • $标志着正则表达式的结束。

我不是正则表达式专家,所以如果我错了请纠正我。