JS中的一个正则表达式

A regular expression in JS

本文关键字:一个 正则表达式 JS      更新时间:2023-09-26

你能帮我理解以下regexp的含义吗:

(?:.*? 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

它来自《最终指南》,我讨厌那本书。一些与regexp匹配和不匹配的示例将不胜感激。

正则表达式为:

(?:    # 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

*?是所谓的惰性量词,它迫使正则表达式引擎在吞下一个字符之前先查看下一个字符——它被使用、过度使用和滥用,这是一种情况:由于下一个角色是一个文字空间,它必须被[^ ]*(任何不是空格的东西,零次或多次)取代,从而完全避免了前瞻。

确定性。正确的

相关文章: