Getting empty matches when using the regular expression \ d *

string = 'Hello _1234_ world _4567_';
string.match(/(\d*)/gi);
//,,,,,,,1234,,,,,,,,,,4567,,

If I modify regext to have \ d +, then I get the correct values: 1234 and 4567. Why in the first case I get all these empty matches.

+3
source share
2 answers

*means 0 or more. +means 1 or more.

\d*corresponds to the empty space between each character, because it is an empty string, i.e. zero digits. When you use \d+, an empty string is no longer a valid match, so you won’t get these extra matches.

+2
source

(\ d *), , 0 , ( , linefeed).

\d +, , , 1 .

+1

Source: https://habr.com/ru/post/1735303/


All Articles