JavaScript正则表达式文字在函数调用之间持续存在 [英] JavaScript regular expression literal persists between function calls

查看:70
本文介绍了JavaScript正则表达式文字在函数调用之间持续存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码:

function func1(text) {

    var pattern = /([\s\S]*?)(\<\?(?:attrib |if |else-if |else|end-if|search |for |end-for)[\s\S]*?\?\>)/g;

    var result;
    while (result = pattern.exec(text)) {
        if (some condition) {
            throw new Error('failed');
        }
        ...
    }
}

这是有效的,除非执行throw语句。在这种情况下,下次调用该函数时,exec()调用将从它停止的地方开始,即使我为它提供了一个新的'text'值。

This works, unless the throw statement is executed. In that case, the next time I call the function, the exec() call starts where it left off, even though I am supplying it with a new value of 'text'.

我可以通过写

var pattern = new RegExp('.....');

var pattern = new RegExp('.....');

相反,但我不明白为什么第一个版本失败了。正则表达式如何在函数调用之间保持不变? (这是在最新版本的Firefox和Chrome中发生的。)

instead, but I don't understand why the first version is failing. How is the regular expression persisting between function calls? (This is happening in the latest versions of Firefox and Chrome.)

编辑完整的测试用例:

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<title>Test Page</title>
<style type='text/css'>
body {
    font-family: sans-serif;
}
#log p {
    margin:     0;
    padding:    0;
}
</style>
<script type='text/javascript'>
function func1(text, count) {

    var pattern = /(one|two|three|four|five|six|seven|eight)/g;

    log("func1");
    var result;
    while (result = pattern.exec(text)) {
        log("result[0] = " + result[0] + ", pattern.index = " + pattern.index);
        if (--count <= 0) {
            throw "Error";
        }
    }
}

function go() {
    try { func1("one two three four five six seven eight", 3); } catch (e) { }
    try { func1("one two three four five six seven eight", 2); } catch (e) { }
    try { func1("one two three four five six seven eight", 99); } catch (e) { }
    try { func1("one two three four five six seven eight", 2); } catch (e) { }
}

function log(msg) {
    var log = document.getElementById('log');
    var p = document.createElement('p');
    p.innerHTML = msg;
    log.appendChild(p);
}

</script>
</head>
<body><div>
<input type='button' id='btnGo' value='Go' onclick='go();'>
<hr>
<div id='log'></div>
</div></body>
</html>

从FF和Chrome第二次调用开始,正则表达式继续为'4',而不是IE7或者Opera。

The regular expression continues with 'four' as of the second call on FF and Chrome, not on IE7 or Opera.

推荐答案

通过正则表达式文字创建的RegExp对象被缓存,但是 new RegExp 始终创建一个新对象。缓存的对象也保存它们的状态,但是管理该方面的规则显然不是很清楚。 Steve Levithan在此博客文章(靠近底部)中谈到了这一点。

RegExp objects that are created by means of a regex literal are cached, but new RegExp always creates a new object. The cached objects also save their state, but the rules governing that aspect are apparently not very clear. Steve Levithan talks about that in this blog post (near the bottom).

这篇关于JavaScript正则表达式文字在函数调用之间持续存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆