I’m trying to learn regex in javascript. While doing testing, my mind get confused on seeing the output.
Can you please explain why my output result array has empty string while using * in pattern and not in +.
PS: The inputs taken are just a sample simple string to learn.
Here’s my sample code:
a='bb bbbb bbb'
a.match(/(bb)*/g) // O/P is ["bb","","bbbb","","bb","",""]
a.match(/(bb)+/g) // O/P is ["bb","bbbb","bb"]
The
*, or Kleene Star, in regular expressions means “zero or more”, so it matches empty string.The
+means “one or more” instead, so it does not match empty string.Given that we have as follows
Let’s represent it like this:
where
^is start of string and$is end of string.Our pattern is
(bb)*. This means that the engine will look for a sequence of twob“zero or more times”. This means the pattern matchesbbif the sequence is found or empty string otherwise.Now step by step I’ll use a dot (
.) to represent regexp analysis.STEPS
In
STEP 1we have pushedbbsince the pattern matched one sequence.In
STEP 2we have pushed""because the pattern matched empty string.In
STEP 3we have pushedbbbbbecause the pattern matched two sequences.STEP 4->STEP 2STEP 5->STEP 1In
STEP 6we have pushed""because the pattern matched empty string since, only onebwas found.Finally you encounter
$token and it matches empty string again.Note that if the string was
"bb bbbb bbbb"the array would have been["bb", "", "bbbb", "", "bbbb", ""]