2

Why doesn't this regexp match all the bs in the string?

'abbbbbbb'.match(/b*/)
#=> #<MatchData "">

whereas this does:

'abbbbbbb'.match(/b+/)
#=> #<MatchData "bbbbbbb">
  • The definition of * is: "The asterisk indicates there is zero or more of the preceding element".
  • The definition of + is: "The plus sign indicates there is one or more of the preceding element".

The only difference between the two operators then is the number of matches that they make. They should both match 7 bs.

sawa
  • 156,411
  • 36
  • 254
  • 350
Robin Smith
  • 239
  • 1
  • 9

1 Answers1

4

It's not a bug and you just answered your question yourself.

The definition of * is: "The asterisk indicates there is zero or more of the preceding element"

So /b*/ matches if there's no b characters at all.

user2422869
  • 670
  • 7
  • 16