here is the code
var str = 'a girl is reading a book!';
var reg = /\w*(?=ing\b)/g;
var res = str.match(reg);
console.log(res);
result is ["read",""] in chrome.
I wanna ask why there is "" in the result.
This is expected behaviour
First read
is captured since it is followed by ing
.Here ing
is only matched..It is never included in the result.
Now we are at the position after read
i.e we would be at i
..here again ing
matches(due to \w*
) and it gives an empty result because there is nothing between read and ing.
You can use \w+(?=ing\b)
to avoid the empty result
/\w*(?=ing\b)/g
Here you are using *
which means none or more, Hence it captures both "read"ing
and ""ing
after it reads the read
part. Use + for one or more.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With