JS中的正则表达式

时间:2012-01-18 13:31:10

标签: javascript regex

您能帮我理解以下正则表达式的含义:

(?:.*? rv:([\w.]+))?

所以,

(?: //the pattern must be in a string, but doesn't return
. //any Unicode character except newline
* //zero or more times
? //zero or one time (how is *? different from just *)
rv: //just "rv:" apparently
[\w //any digit, an underscore, or any Latin-1 letter character
.] //...or any unicode character (are Latin-1 characters not Unicode?)
..))? //all that zero or one time

这是来自“权威指南”,我讨厌那本书。 一些与正则表达式匹配和不匹配的示例将非常感激。

1 个答案:

答案 0 :(得分:2)

正则表达式是:

(?:    # begin non capturing group
.*?    # any character, zero or more times, but peek and stop if the next char is
       # a space (" "); then look for
rv:    # literal "rv:", followed by
(      # begin capturing group
[\w.]  # any word character or a dot (the dot HAS NO special meaning in a character class),
+      # once or more,
)      # end capturing group
)      # end non capturing group
?      # zero or one time

*?是所谓的懒惰量词,它强制正则表达式引擎在吞下一个字符之前偷看下一个字符 - 它被使用,过度使用和滥用,这是一种情况:因为下一个字符它是一个文字空间,必须用[^ ]*(任何不是空格,零次或多次)代替,这样就完全避免了先行。

定形。右。