Javascript 为什么匹配函数只返回第一个字母?

Javascript 为什么匹配函数只返回第一个字母?,javascript,regex,Javascript,Regex,如果元字符?与前面的元素零或一次匹配,则 为什么 返回[“a”] 但是 返回[“”] 因为这是第一场比赛。regex首先尝试在位置0进行匹配,其中regex#1与a匹配,regex#2与空字符串匹配。然后它尝试在位置1进行匹配,其中regex#1与空字符串匹配,regex#2与字母b匹配。最后,它尝试在位置3进行匹配,其中两个正则表达式都匹配空字符串 将返回的匹配项与全局标志进行比较: > "ab".match(/a?/) ["a"] > "ab".match(/a?/g) ["a"

如果元字符
与前面的元素零或一次匹配,则

为什么

返回
[“a”]

但是

返回
[“”]


因为这是第一场比赛。regex首先尝试在位置0进行匹配,其中regex#1与
a
匹配,regex#2与空字符串匹配。然后它尝试在位置1进行匹配,其中regex#1与空字符串匹配,regex#2与字母
b
匹配。最后,它尝试在位置3进行匹配,其中两个正则表达式都匹配空字符串

将返回的匹配项与全局标志进行比较:

> "ab".match(/a?/)
["a"]
> "ab".match(/a?/g)
["a", "", ""]
> "ab".match(/b?/)
[""]
> "ab".match(/b?/g)
["", "b", ""]
为什么不在第一种情况下返回[“”]

由于机制的原因。当尝试在某个位置进行匹配时,引擎将尝试根据字符串的字母贪婪地1测试正则表达式的所有字母。当它使用该方法到达正则表达式的末尾时,匹配成功。当一个字母不适合时,它会尝试返回正则表达式,查看是否可以进行任何省略-当使用诸如
*
之类的修饰符时-或者需要考虑替代(
|
)时,然后从那里继续

示例:在
“ab”
的位置0处匹配
/b?/

示例:在“abc”

// - "": ✓ /a/-“a”:✓ /ab/-“ab”:✓ /ab(b)/“abc”: /ab(bc)?/-“ab”:✓ - 成功(正则表达式结束)
1:至少通常是这样。如果你想控制精确的匹配行为,许多正则表达式风格也提供了量词。例如,
/ab???(bc)?/
匹配
中的
abc
“abc”
我不知道是否可以用下面的类比更好地解释它

想象一下,一个友好的快餐收银员,一队饥饿的顾客,一条装满汉堡的传送带。她把汉堡一个一个地提供给每位顾客,直到他们都满意为止(如果剩下一些汉堡,那没关系)。然后,她写下每个客户所拥有的物品清单,并将该清单交给她的经理。出纳是正则表达式引擎,汉堡是输入字符串中的符号,客户是正则表达式的子表达式。经理是匹配函数

例如,当将
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! (“[a]”、“b+”和“.”排队) 收银员:嗨,“[a]”,您要“a”吗? [a] :当然可以,谢谢! C:你也要b吗? [a] :不,谢谢,我很好。 嗨,b+,你要b吗? b+:当然。 你想再来一个“b”吗? 是的,我饿了。 C:我也可以给你“C”吗? b+:不是我喜欢的类型。 嗨,“.”,我只剩下一个“C”。 :我不管是什么,只要给我就行了。 C:都准备好了!看来我会得到这份工作的! 如果出纳员不能满足顾客的要求,她有权打电话给前一位顾客,要求他们归还所得到的东西。这被称为“回溯”。考虑:

"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... “abx”对抗/+[xyz]/ C:嗨,“.+”,你想要“a”吗? .+:好吃! C:b怎么样? .+:好吃! C:那么“x”呢? .+:好吃! 我的皮带空了!(.+去) 嗨,“[xyz]”,恐怕我已经卖完了。 那是不可能的。我可以见经理吗? 等等,我想我们可以解决它!(称“+”) C(对“+”):对不起,伙计,你知道,那个讨厌的家伙。。。 不知道你能不能把你最后一个还给我? .+:没问题。。。(返回“x”) C(对“[xyz]”):我有东西要给你。你吃“x”吗? 如果你想在这个国家完成任何事情 你得抱怨到脸色发青为止 (得到他的“x”并怒不可遏) 天哪,多好的一天啊。。。 现在回到您的示例:

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. 第一幕“ab”对抗/a?/ 汉堡:a和b,顾客:a? 嗨,a?你要a吗? 当然可以,谢谢。 C:我也可以给你b吗? 不,谢谢,我很好。 经理:我现在需要库存报告! C:给你:“a?”有“a”,我们还有“b”和“C”。 第二场。“ab”反对/b?/ 汉堡:a和b,顾客:b? 嗨,b?你要a吗? 不用了,谢谢,不过没问题。(继续)。 M:地位? C:“b?”什么都没有,就走了。a、 b,c仍然在那里。
所以,基本上,
b?
是一个非常好的人(不是特别饿),即使收银员没有东西给他,他也很高兴。如果他是队列中唯一的一个,那就是她的幸运日

那么为什么不在第一种情况下返回[“”]?(如果有人问:为什么不?
是贪婪的,所以它将尽可能地匹配它位于+1的位置,但是“位置0”对我来说似乎并不清楚。特别是当你考虑<代码>”时,匹配(/a/g)< /代码>。在我看来,这是答案的一半。“位置0”是字符串的开头,即
“ab”
a
的左边。你说得对,空匹配令人困惑:-)想象一下最快乐的场景(对于客户、收银员,尤其是经理):“对抗/*/@user3119308:;))每一个类比都只是一个类比。不要太认真@用户3119308:我不认为那是幸福的。这家商店什么也不卖,即使它卖了,也有一个饥肠辘辘的顾客挡住了一切… // - "": ✓ /a/ - "a": ✓ - succeed (end of regex) // - "": ✓ /a/ - "a": ✓ /ab/ - "ab": ✓ /ab(b)/ - "abc": × /ab(bc)?/ - "ab": ✓ - succeed (end of regex) ("[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.