2011-08-29 81 views

回答

2

通过捕捉其他一切捕获组之前就可以得到抵消一些捕获组:

var pattern = /(^.*abc)(d)e/; 
var somestring = 'abcde'; 
var match = somestring.match(pattern); 
var index = match[1].length; // this is the offset of `d` in the string 

,或虽未捕捉对象字符串的开头:

var pattern = /(abc)(d)e/; 
var somestring = 'abcde'; 
var match = somestring.match(pattern); 
var index = match[1].length + match.index; // this is the offset of `d` in the string 

match.index是在字符串中开始匹配的索引。

+0

或者只是'3 + match.index',给定模式具有恒定的大小:-) – Bergi