2012-03-17 60 views
0

我看到這行代碼和正則表達式只是嚇壞了我......有人可以向我解釋此RegEx嗎?

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

能有人請解釋一點一點它做什麼? 感謝,G

+0

難道你不能打破自己和谷歌每個組件的正則表達式?這很簡單。 – Blender 2012-03-17 02:01:45

+0

我開始了,但是'[\ w \ W]'對我來說沒有意義。好奇看看你得到了什麼答案。 – 2012-03-17 02:05:14

+0

@Blender你是對的。其實我正在閱讀關於它的MDN。 – Gnijuohz 2012-03-17 02:09:13

回答

2

這是我可以提取:

  • ^開頭的字符串。
  • (?:不匹配組。
  • [^#<]*任何數量的連續字符不是#<
  • (<[\w\W]+>)一個匹配字符串的組,如<anything_goes_here>
  • [^>]*任意數量的字符順序不是>

|後面的部分表示如果第一個失敗,則嘗試使用第二個正則表達式。那一個是#([\w\-]*)

  • ##字符匹配。不那麼複雜。
  • ([\w\-]*)是匹配任意數量的單詞字符或短劃線的組。基本上Things-of-this-form
  • $標誌着正則表達式的結束。

我不是正則表達式親,所以請糾正我,如果我錯了。

+0

我不習慣兩個特殊字符同時出現。像'\ w \ W',不應該先匹配一個單詞字符,然後是非單詞字符(如第一個答案所示),而不是匹配任何東西?與\ w \ -'相同的問題。 – Gnijuohz 2012-03-17 02:45:46

+0

當事物在[]括號中時,它們沒有任何特定的順序。 '[]'括號表示字符集,用於包含/排除正則表達式中的東西(比如'[az] +'與'[zyxvutsrqponmlkjihgfedcba] +'匹配的東西]' – Blender 2012-03-17 02:47:02

+0

aha ...它們在[ ]'。我明白了......我很笨! – Gnijuohz 2012-03-17 02:47:45

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 
+0

那麼,你的解釋在哪裏? – 2012-03-17 02:09:05

+1

這是,呃,不完全是'閱讀友好'? – Gnijuohz 2012-03-17 02:12:54

+0

這是告訴你,一點一滴的正則表達式。這正是你要求的。 – david 2012-03-17 02:14:27

相關問題