简体   繁体   English

有人可以向我解释此正则表达式吗?

[英]Can somebody explain this RegEx to me?

I see this line of code and the regular expression just panics me... 我看到这行代码和正则表达式让我感到恐慌...

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

Can someone please explain little by little what it does? 有人可以一点一点解释它的作用吗? Thanks,G 谢谢,G

^(?:[^#<]*(<[\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

Here's what I can extract: 这是我可以提取的内容:

  • ^ beginning of string. ^字符串的开头。
  • (?: non-matching group. (?:不匹配的组。
  • [^#<]* any number of consecutive characters that aren't # or < . [^#<]*任何非#<的连续字符。
  • (<[\\w\\W]+>) a group that matches strings like <anything_goes_here> . (<[\\w\\W]+>)与诸如<anything_goes_here>类的字符串匹配的组。
  • [^>]* any number of characters in sequence that aren't a > . [^>]*序列中任意多个不是>的字符。

The part after the | |后的部分 denotes a second regex to try if the first one fails. 表示如果第一个正则表达式失败,则尝试第二个正则表达式。 That one is #([\\w\\-]*) : 那是#([\\w\\-]*)

  • # matches the # character. ##字符匹配。 Not that complex. 没那么复杂。
  • ([\\w\\-]*) is a group that matches any number of word characters or dashes. ([\\w\\-]*)是与任意数量的单词字符或破折号匹配的组。 Basically Things-of-this-form 基本上Things-of-this-form
  • $ marks the end of the regex. $标志正则表达式的结尾。

I'm no regex pro, so please correct me if I am wrong. 我不是regex专业人士,所以如果我写错了,请纠正我。

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

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