Fix the problem when no hit at the end of the string.

Add another else if statement to avoid string not get pushed when there is no match at the end of the string.