在Javascript中,是否可以在字符串中找到与正则表达式匹配的所有子字符串的起始和结束索引?
功能签名:
function getMatches(theString, theRegex){
//return the starting and ending indices of match of theRegex inside theString
//a 2D array should be returned
}
Run Code Online (Sandbox Code Playgroud)
例如:
getMatches("cats and rats", /(c|r)ats/);
Run Code Online (Sandbox Code Playgroud)
应该返回数组[[0, 3], [9, 12]],该数组表示字符串中"cats"和"rats"的起始和结束索引.
sac*_*een 10
使用match找寻符合正则表达式的所有子.
> "cats and rats".match(/(c|r)ats/g)
> ["cats", "rats"]
Run Code Online (Sandbox Code Playgroud)
现在您可以使用indexOf和length查找开始/结束索引.
| 归档时间: |
|
| 查看次数: |
5859 次 |
| 最近记录: |