- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
在使用 JavaScript:权威指南 学习 javascript 中的正则表达式时,我对这段话感到困惑:
But /a+?/ matches one or more occurrences of the letter a, matching as few characters as necessary. When applied to the same string, this pattern matches only the first letter a.
…
Now let’s use the nongreedy version: /a+?b/. This should match the letter b preceded by the fewest number of a’s possible. When applied to the same string “aaab”, you might expect it to match only one a and the last letter b. In fact, however, this pattern matches the entire string, just like the greedy version of the pattern.
为什么会这样?
这是书中的解释:
This is because regular-expression pattern matching is done by finding the first position in the string at which a match is possible. Since a match is possible starting at the first character of the string,shorter matches starting at subsequent characters are never even considered.
我不明白。谁能给我更详细的解释?
最佳答案
好的,现在您有了搜索空间“aaabc”和模式/a+?b/
/a+?b/匹配“a”吗?没有。
/a+?b/匹配“aa”吗?没有。
/a+?b/匹配“aaa”吗?没有。
/a+?b/是否匹配“aaab”?是的。
关于javascript - 由于 "a+?"是惰性的,为什么 "a+?b"匹配 "aaab"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24900026/
在使用 JavaScript:权威指南 学习 javascript 中的正则表达式时,我对这段话感到困惑: But /a+?/ matches one or more occurrences of t
我是一名优秀的程序员,十分优秀!