如果元字符?
与前面的元素零或一次匹配,那么
为什么
"ab".match(/a?/)
返回["a"]
,
但
"ab".match(/b?/)
返回[""]
答案 0 :(得分:8)
因为那是第一场比赛。正则表达式首先尝试匹配位置0,其中正则表达式#1匹配a
,正则表达式#2匹配空字符串。然后它尝试匹配位置1,其中正则表达式#1匹配空字符串,正则表达式#2匹配字母b
。最后,它尝试匹配位置3,其中两个正则表达式都匹配空字符串。
将返回的匹配与全局标记进行比较:
> "ab".match(/a?/)
["a"]
> "ab".match(/a?/g)
["a", "", ""]
> "ab".match(/b?/)
[""]
> "ab".match(/b?/g)
["", "b", ""]
为什么不在第一种情况下返回[“”]?
由于backtracking的机制。当试图在某个位置匹配时,引擎将尝试贪婪 1 测试正则表达式的所有字母与字符串的字母。当它使用该方法到达正则表达式的末尾时,匹配成功。当一个字母不适合时,它会尝试返回正则表达式以查看是否可以进行任何遗漏 - 使用*
或?
等修饰符 - 或替代({{1} })需要考虑,然后从那里继续。
示例:在|
的位置0匹配/b?/
:
// - "": ✓ /b/ - "a": × /b?/ - "": ✓ - succeed (end of regex) ^ means here that the "b" token is omitted
示例:在"ab"
的位置0匹配/a?/
:
// - "": ✓ /a/ - "a": ✓ - succeed (end of regex)
示例:在"ab"
/ab?(bc)?/
// - "": ✓ /a/ - "a": ✓ /ab/ - "ab": ✓ /ab(b)/ - "abc": × /ab(bc)?/ - "ab": ✓ - succeed (end of regex)
1:通常,至少。如果要控制完全匹配行为,许多正则表达式风格还提供lazy或possessive的量词。例如,"abc"
与/ab??(bc)?/
abc
匹配
答案 1 :(得分:0)
我想知道我是否可以通过以下类比更好地解释它。
想象一下,一个友好的快餐收银员,一群饥肠辘辘的顾客和一种装满汉堡的传送带。她一个接一个地向每个顾客提供汉堡,直到所有顾客都满意为止(如果留下一些汉堡,它并不重要)。然后,她写下每个客户的清单,并将此清单提供给她的经理。收银员将是正则表达式引擎,汉堡是输入字符串中的符号,客户是正则表达式子表达式。经理是匹配功能。例如,当abbc
与/[a]b+./
匹配时,场景会像这样播放:
("[a]", "b+" and "." stand the queue) Cashier: Hi, "[a]", would you like "a"? [a]: Sure, thanks! C: would you also like "b"? [a]: No, thanks, I'm fine (goes). C: Hi, "b+", would you like "b"? b+: Sure. C: Would you like another "b"? b+: Yes, I'm hungry. C: Can I offer you "c" also? b+: Not my kind of thing (goes). C: Hi, ".", I have only one "c" left. .: I don't care what it is, just gimme it (goes). C: All served! Looks like I'll get the job!
如果收银员无法满足顾客,她有权打电话给前一个并要求他们回馈他们所拥有的东西。这被称为"回溯"。考虑:
"abx" against /.+[xyz]/ C: Hi, ".+", would you like "a"? .+: Yum-yum! C: How about "b"? .+: Yum-yum! C: And "x"? .+: Yum-yum! C: My belt is empty! (.+ goes) C: Hi, "[xyz]", I'm afraid I'm sold out. [xyz]: That's out of the question. Can I see the manager? C: Wait, I think we can sort it out! (calls ".+") C (to ".+"): Sorry pal, you know, this nasty guy over there... I wonder if you could you give me back your last one? .+: No prob... (gives "x" back) C (to "[xyz]"): I've got something for you. Do you eat "x"? [xyz]: If you want to get anything done in this country you've got to complain until you are blue in the mouth (gets his "x" and goes in a rage) C: Gosh, what a day...
现在回到你的例子:
Scene I. "ab" against /a?/ Burgers: a and b, customer: a? C: Hi, "a?" would you like "a"? a?: Sure, thanks. C: Can I offer you "b" also? a?: No, thanks, I'm fine (goes). Manager: I need the inventory report, now! C: Here you go: "a?" got "a", we have "b" and "c" left. Scene II. "ab" against /b?/ Burgers: a and b, customer: b? C: Hi, "b?" would you like "a"? b?: No thanks, but that's no problem. (goes). M: Status? C: "b?" got nothing and went. a, b, c are still there.
所以,基本上b?
是一个非常好(而不是特别饥饿)的人,即使收银员对他没有任何帮助,他也很高兴。如果他是队列中唯一的一员,那就是她的幸运日!