JavaScript不替换所有元素

JavaScript不替换所有元素,javascript,jquery,Javascript,Jquery,我正在尝试替换表单元素的索引。我有以下几点 var test = "<input name='[1].Id' value='598' type='hidden' /><input name='[1].OrderItemId' value='867' type='hidden' />"; alert(test.replace('[1]', '[2]')); var测试=”; 警报(测试。替换(“[1]”、“[2]”); 我得到了奇怪的结果。第一个隐藏字段

我正在尝试替换表单元素的索引。我有以下几点

    var test = "<input name='[1].Id' value='598' type='hidden' /><input name='[1].OrderItemId' value='867' type='hidden' />";
    alert(test.replace('[1]', '[2]'));
var测试=”;
警报(测试。替换(“[1]”、“[2]”);
我得到了奇怪的结果。第一个隐藏字段替换为第二个隐藏字段,并忽略该字段

我的回答是这样的:

"<input name='[1].Id' value='598' type='hidden' /><input name='[2].OrderItemId' value='867' type='hidden' />"
“”
编辑:

好的,谢谢这些方法在我的简单示例中起作用。然而实际上,我的字符串要复杂一点。以下是“var lastRow”的内容


地址1
这里是js函数

    $('#addNewAddress').click(function (event) {

        event.preventDefault();
        var length = $('.table-item-address tbody').find('tr').length;
       var previousLength = length - 1;
        var previousIndex = "/\[" + previousLength + "\]/g";
        var currentIndex = "[" + length + "]";
        var lastRow = $('.table-item-address tbody tr').last();
alert(lastRow.html()); // html is shown above
        var newRow = lastRow.html().replace(previousIndex, currentIndex);
        $('.table-item-address tr').last().after('<tr>' + newRow + '</tr>');
        AdjustValues();
    });
$('#addNewAddress')。单击(函数(事件){
event.preventDefault();
变量长度=$('.table item address tbody')。查找('tr')。长度;
var previousLength=长度-1;
var previousIndex=“/\[”+previousLength+“\]/g”;
var currentIndex=“[”+长度+“]”;
var lastRow=$('.table item address tbody tr').last();
警报(lastRow.html());//上面显示的是html
var newRow=lastRow.html().replace(previousIndex,currentIndex);
$('.table item address tr').last()之后(''+newRow+'');
调整值();
});
参见此处:
查看第三个示例,其中他们讨论了全局替换

请参见此处:

检查第三个示例,其中讨论了JavaScript中的全局替换,将字符串作为第一个参数传递给
replace()
将只替换第一个出现的字符串。您需要使用带有全局标志的正则表达式:

test.replace(/\[1\]/g, '[2]');
额外的反斜杠(
\
)从括号(
[
]
)中退出


编辑:响应OP的编辑,如果您想动态构建正则表达式,您不能使用正则表达式文字-这是由前斜杠(
/
)分隔的东西,而不是在编辑中使用引号(
)。您将字符串传递到
替换()
,我将传递正则表达式文字。使用

// The first argument is the regex, the second is a string of flags.
var previousIndex = new RegExp("\\[" + previousLength + "\\]", "g");

// Then, it's exactly the same as before.
// The second argument to replace is still a string.
var newRow = lastRow.html().replace(previousIndex, currentIndex);

注意字符转义的区别。

在JavaScript中,将字符串作为第一个参数传递给
replace()
只会替换第一个出现的字符串。您需要使用带有全局标志的正则表达式:

test.replace(/\[1\]/g, '[2]');
额外的反斜杠(
\
)从括号(
[
]
)中退出


编辑:响应OP的编辑,如果要动态构建正则表达式,则不能使用正则表达式文字-这是由前斜杠(
/
)分隔的内容,而不是在编辑中用引号(
)分隔的内容。您将一个字符串传递到
replace()
,我将传递一个正则表达式文本。使用以下选项修复您的:

// The first argument is the regex, the second is a string of flags.
var previousIndex = new RegExp("\\[" + previousLength + "\\]", "g");

// Then, it's exactly the same as before.
// The second argument to replace is still a string.
var newRow = lastRow.html().replace(previousIndex, currentIndex);

注意字符转义的差异。

将替换更改为此
替换(/[1]/g,“[2]”)

这是一个全球性的替代。然而,我不确定。是ID/名称的合法字符。

将替换更改为this
replace(/[1]/g,“[2]”)

这是一个全球性的替代。然而,我不确定。是ID/名称的合法字符。

谢谢,这是我的示例。是的,你应该得到正确的答案。希望你能快速浏览一下编辑。看看我的编辑。有一个更干净的方法可以做到这一点——现在就开始做。忽略我之前的评论。我忘了JavaScript不支持lookback(和)!谢谢,这是我的例子。是的,你应该得到正确的答案。希望你能快速浏览一下编辑。看看我的编辑。有一个更干净的方法可以做到这一点——现在就开始做。忽略我之前的评论。我忘了JavaScript不支持lookback(和)!