What strings does this regex matches ? (a|b)*

regepxal is showing infinite matches and shows “baaaaaaa” as a match. Doesn’t (a|b) means a or b?

  • means match 0 or more times. So, it should be matching aa,aaa,bb,bbb, , etc?
    I’m not getting how baaaa is a match. Can anyone clarify please?

Each of those characters is a or b, so they’re all matched. Are you wanting to only match one or the other?

I feel I’m not getting it. I feel I’m missing something that you have good grasp of. You choose a or b and repeat, so aaaa, bbbb should be only matches. Could you help me out?

What is the exact regular expression?

Is it /(a|b)*/g?

If so, the g flag will find all the instances of one character of a or b. The following are the actual matches for the string baaaaaaa

b
a
a
a
a
a
a
a

Maybe you mean a different regular expression?

Thank you. The perfect way to think about this for me is this:

(X)*= X X X X X X

Here X=a or b

So,

a or b then a or b then a or b then…