<html>
<body>
<script type="text/javascript">
var str = "Visit W3School, W3School is a place to study web technology.";
var patt = new RegExp("W3School");
var result;
while ((result = patt.exec(str)) != null) {
document.write(result);
document.write("<br />");
document.write(patt.lastIndex);
document.write("<br />");
}
</script>
</body>
</html>
var patt = new RegExp("W3School");处原先是var patt = new RegExp("W3School", "g");
去掉g为什么老卡啊,两者区别是什么?
------解决方案--------------------
要全局匹配,要不会死循环
exec 方法受参数 g 的影响。若指定了 g,则下次调用 exec 时,会从上个匹配的 lastIndex 开始查找。