te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - Why does String.match(d* ) return an empty string? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why does String.match(d* ) return an empty string? - Stack Overflow

programmeradmin2浏览0评论

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));
// [""]

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));
// [""]
Share Improve this question asked May 20, 2015 at 21:11 tbutmantbutman 1753 silver badges8 bronze badges 2
  • 2 The * character matches zero or more occurrences of the character in a row, the + character is similar but matches one or more. – GillesC Commented May 20, 2015 at 21:15
  • 1 The string is checked for a match from left-to-right. Since \d* can match an empty string right at index 0, it returns a match right there. – nhahtdh Commented May 21, 2015 at 5:51
Add a ment  | 

4 Answers 4

Reset to default 10

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

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.

* means 0 or more, so it's matching 0 times. You need to use + for 1 or more. By default it's greedy, so will match 100:

var str = 'one to 100';
var regex = /\d+/;
console.log(str.match(regex));
// ["100"]

As @StriplingWarrior said below, the empty string is the first match, hence it is being returned. I would like to add that you can tell what the regex is matching by noticing the 'index' field which the function match returns. For example, this is what I get when I run your code in Chrome:

["", index: 0, input: "one to 100"]
发布评论

评论列表(0)

  1. 暂无评论