I have asked a question here about js, regex, quantifiers and global search. I've understood finally how this works, but, let's take a concrete example and then I`ll write my question.
Based on the same example
var str = 'ddd';
var r = /d*/g;
console.log(str.match(r))
it outputs this array: ["ddd", ""]
I understand that the first item in the array is because it matches the letter d
and the last item (that empty string) is because it matches the end of the string, which is nothing, so * makes sense because it matches 0 or more occurrences...
So, my questions are:
- Why this is happening?
- Why it just have to query the end of the string to finally obtain a true matching?
In my opinion, the end of the string(ddd) should not be queried; because it's not like my string is containing an empty space at the end 'ddd '. If my string was empty, it was logical to match, but not in this case. My logic here is this:
for every character in the string, do this search/regex (d*) ...so why does it just continue with the end of the string? It should stop on the last charachter of my string, which in this case is d
...