【发布时间】:2018-09-14 06:02:38
【问题描述】:
试图引用此 (Return positions of a regex match() in Javascript?) 以提取特定字符串中匹配项的开始和结束位置,但不知何故它只返回 1 个匹配项。不知道我做错了什么。
var str = "abc <span djsodjs> ajsodjoasd <spandskds>";
var patt = /.*(<span.*>).*/igm;
while (match = patt.exec(str)) {
console.log(match.index + ' ' + patt.lastIndex);
}
返回
0 41
预期:
0 41
4 17
30 40
【问题讨论】:
-
您应该避免使用正则表达式来解析 HTML 内容。
标签: javascript regex