【发布时间】:2012-08-15 10:19:41
【问题描述】:
可能重复:
How can I match multiple occurrences with a regex in JavaScript similar to PHP's preg_match_all()?
在 Javascript 中,是否可以在一个字符串中找到匹配正则表达式的所有子字符串的开始和结束索引?
函数签名:
function getMatches(theString, theRegex){
//return the starting and ending indices of match of theRegex inside theString
//a 2D array should be returned
}
例如:
getMatches("cats and rats", /(c|r)ats/);
应该返回数组[[0, 3], [9, 12]],表示字符串中“cats”和“rats”的开始和结束索引。
【问题讨论】:
标签: javascript regex