Why does String.match( / \d*/ ) return an empty string?

前端 未结 4 1999
挽巷
挽巷 2020-11-27 23:34

Can someone help me to understand why using \\d* returns an array containing an empty string, whereas using \\d+ returns [\"100\"] (as expected). I get why the \\d+ works, b

4条回答
  •  执笔经年
    2020-11-28 00:17

    As @StriplingWarrior said below, the empty string is the first match, hence it is being returned. I would like to add that you can tell what the regex is matching by noticing the 'index' field which the function match returns. For example, this is what I get when I run your code in Chrome:

    ["", index: 0, input: "one to 100"]
    

提交回复
热议问题