Can someone help me to understand why using \d* returns an array containing an empty string, whereas using \d+ returns ["100"] (as expected). I get why the \d+ works, but don't see why exactly \d* doesn't work. Does using the * cause it to return a zero-length match, and how exactly does this work?
var str = 'one to 100';
var regex = /\d*/;
console.log(str.match(regex));
// [""]
Remember that match
is looking for the first substring it can find that matches the given regex.
*
means that there may be zero or more of something, so \d*
means you're looking for a string that contains zero or more digits.
If your input string started with a number, that entire number would be matched.
"5 to 100".match(/\d*/); // "5"
"5 to 100".match(/\d+/); // "5"
But since the first character is a non-digit, match()
figures that the beginning of the string (with no characters) matches the regex.
Since your string doesn't begin with any digits, an empty string is the first substring of your input which matches that regex.
/\d*/
means "match against 0 or more numbers starting from the beginning of the string".
When you start the beginning for your string, it immediately hits a non-number and can't go any further. Yet this is considered a successful match because "0 or more".
You can try either "1 or more" via
/\d+/
or you can tell it to match "0 or more" from the end of the string:
/\d*$/
In Python, there is the findall()
method which returns all parts of the string your regular expression matched against.
re.findall(r'\d*', 'one to 100')
# => ['', '', '', '', '', '', '', '100', '']
.match()
in JavaScript, returns only the first match, which would be the first element in the above array.
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