开发者

Getting empty matches when using regular expression \d*

string = 'Hello _1234_ world _4567_';
string.match(/(\d*)/gi);
//,,,,,,,1234,,,,,,,,,,4567,,
开发者_如何学Python

If I change regext to have \d+ then I get proper values: 1234 and 4567. Why in the first case I am getting all those empty matches.


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

\d* matches the empty space between each character because this is the empty string, i.e. zero digits. When you use \d+ the empty string is no longer a valid match so you don't get these extra matches.


Because you have (\d*) which means capture every time there is 0 or more digits, which means every character (even some non visible characters like a linefeed) matches.

With \d+, you are saying match where there are 1 or more digits.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜