gpt4 book ai didi

javascript - JS中的正则表达式

转载 作者:行者123 更新时间:2023-11-30 18:33:36 26 4
gpt4 key购买 nike

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

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

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

最佳答案

正则表达式是:

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

*? 是所谓的惰性量词,它强制正则表达式引擎在吞下一个字符之前先查看下一个字符——它被使用、过度使用和滥用,这是一种情况:由于下一个字符是文字​​空格,因此必须将其替换为 [^ ]*(任何不是空格的内容,零次或多次),这完全避免了前瞻。

确定性的。对。

关于javascript - JS中的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8910957/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com