Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does String.match( / \d*/ ) return an empty string?

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));
// [""]
like image 210
tbutman Avatar asked May 20 '15 21:05

tbutman


2 Answers

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.

like image 71
StriplingWarrior Avatar answered Oct 05 '22 02:10

StriplingWarrior


/\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*$/

Find all in Python

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.

like image 29
14 revs, 12 users 16% Avatar answered Oct 05 '22 02:10

14 revs, 12 users 16%