我在名称属性得到更新一种形式,但使用多维值如下问题IM:查找和替换[括号]表达的第n次出现在字符串
<input type="text" name="questions[0][question]" />
<input type="text" name="questions[0][order]" />
<input type="text" name="questions[0][active]" />
<input type="text" name="answers[0][1][answer]" />
<input type="text" name="answers[0][2][answer]" />
<input type="text" name="answers[0][3][answer]" />
<input type="text" name="questions[1][question]" />
<input type="text" name="questions[1][order]" />
<input type="text" name="questions[1][active]" />
etc...
我需要改变。
/(?<=\[)[^\]]*(?=\])/g
:方括号用JavaScript没有
无论他们是在什么位置内的值 我曾尝试使用下面的正则表达式匹配的方括号之间的值尝试
但这匹配所有事件,我需要做的是以某种方式找到并替换第n次出现。
或者,如果有另一种方法来查找和替换方括号内的值,而不使用正则表达式,我全是耳朵。
在此先感谢
解决
这最后的代码如下:
$('input', this).each(function(){
var name = $(this).attr('name');
var i = 0;
$(this).attr('name', name.replace(/\[.+?\]/g,function (match, pos, original) {
i++;
return (i == 1) ? "[THE REPLACED VALUE]" : match;
}));
});
我不是什么你试图做完全清楚!使用上面的示例可以显示预期的输出... – ManseUK
欢迎使用StackOverflow。我发现这个问题没有得到太多的观点,所以我编辑了标题,清楚地反映了你所问的问题 - 如何找到并替换方括号中的第n个表达式。 –