Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/475.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript.replace()无法在Chrome中全局运行_Javascript_Jquery_Plugins_Methods_Replace - Fatal编程技术网

Javascript.replace()无法在Chrome中全局运行

Javascript.replace()无法在Chrome中全局运行,javascript,jquery,plugins,methods,replace,Javascript,Jquery,Plugins,Methods,Replace,我一直在开发一个简单的jQuery插件,它将基本上为json数据模板。它允许您提供一个html模板url、数据url和显示数据的目标。我遇到的问题是,当我这样做时: var regex = new RegExp( '[['+ key +']]', 'g' ); tplHTML = tplHTML.replace( regex, this[key] ); 它只是替换它找到的第一个案例,而不是遵循全局标志 这是调用插件的页面,同时显示数据: <!DOCTYPE html> <he

我一直在开发一个简单的jQuery插件,它将基本上为json数据模板。它允许您提供一个html模板url、数据url和显示数据的目标。我遇到的问题是,当我这样做时:

var regex = new RegExp( '[['+ key +']]', 'g' );
tplHTML = tplHTML.replace( regex, this[key] );
它只是替换它找到的第一个案例,而不是遵循全局标志

这是调用插件的页面,同时显示数据:

<!DOCTYPE html>
<head>
<title></title>

<link href='http://fonts.googleapis.com/css?family=Open+Sans:400,300,700,600' rel='stylesheet' type='text/css'>

<style>
* { font-family: 'Open Sans', sans-serif; color: #666; font-weight: 100; line-height: 150%; font-size: 1.0em; }
.container { width: 960px; margin: 0 auto; padding: 10px; border: 1px solid #ccc; }
    .left { width: 460px; float: left; }
    .right { width: 400px; float: right; }

td, th { text-align: left; vertical-align: top; border: 1px solid #f5f5f5; padding: 10px; font-size: .8em; }
.clear { clear: both; }
</style>

</head>
<body>

<div class="container">
    <div class="left">
        <table id="users">
            <tr>
                <th>ID</th>
                <th>Name</th>
                <th>Email</th>
                <th>Phone</th>
            </tr>
        </table>
    </div>
    <div class="right">
        <table id="comments">
            <tr>
                <th>ID</th>
                <th>User id</th>
                <th>Comment</th>
                <th>Date</th>
            </tr>
        </table>
    </div>

    <div class="clear"></div>
</div>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="js/userData.js"></script>
<script src="js/templatize.ajax.js"></script>

<script>
$('#users').templatize({
    itemsURL: 'process/ajaxUsers.php?l=5&order=ASC',
    tplURL: 'tpl/usersTpl.php',
    targetHTML: '#users'
});

$('#comments').templatize({
    itemsURL: 'process/ajaxComments.php?l=5&order=ASC',
    tplURL: 'tpl/commentTpl.php',
    targetHTML: '#comments'
});
</script>

</body>
</html>
最后是模板代码:

<tr><td>[[id]]</td><td><a href="user.php?id=[[id]]">[[name]]</a></td><td>[[email]]</td><td>[[phone]]</td></tr>
[[id][[email][[phone]]
我使用的标签结构格式与smarty标签类似。我绞尽脑汁想了一段时间,研究了各种文档,但没有找到适合我的解决方案。任何帮助都将不胜感激

总的来说,我只需要找出一种方法来获取javascripts.replace()方法,不仅替换匹配的第一个实例,而且替换所有实例。我尝试了一个版本,它似乎只与Firefox where.replace兼容(搜索、替换、标志)


谢谢

首先,我认为您希望您的正则表达式使用变量
key
而不是字符串
“key”
,因此它应该是:

new RegExp( '[[' + key + ']]', 'g' )
其次,
[
在正则表达式中具有特殊含义,因此需要对其进行转义:

new RegExp( '\\[\\[' + key + ']]', 'g' )

因为你是从一个字符串构建正则表达式,你需要一个双反斜杠来转义每个
[
,这样对于
,比如说
“id”
,得到的正则表达式将是
\[\[id]]

,首先,我认为你是想让正则表达式使用变量
,而不是字符串
“key”
,所以应该是:

new RegExp( '[[' + key + ']]', 'g' )
其次,
[
在正则表达式中具有特殊含义,因此需要对其进行转义:

new RegExp( '\\[\\[' + key + ']]', 'g' )

因为您是从一个字符串构建正则表达式,所以需要一个双反斜杠来转义每个
[
,这样对于
键,比如
“id”
,生成的正则表达式将是
\[\[id]]

谢谢你,n,你说的对,我一直在用各种方法测试它,希望我中了彩票,然后在发到这里之前忘了把它改回来。谢谢你,伙计!!!这已经困扰了我一段时间!!!谢谢你,n,你说的对,我一直在用各种方法测试它,希望我能做到的中了彩票,然后在发到这里之前忘了把它换回来。谢谢老兄!!!这已经困扰了我一段时间了!!!