function DocListFunc_RefreshFieldIndex(tbInfo, index, tagName, optTB){
var optTR = optTB ? optTB.rows[index] : tbInfo.DOMElement.rows[index];
var fields = optTR.getElementsByTagName(tagName);
for(var i=0; i<fields.length; i++){
var fieldName, parentIndex = optTB.getAttribute('KMSS_ParentIndex');
if (parentIndex == null || parentIndex == '') {
fieldName = fields[i].name.replace(/(\d+)(\D+)$/g, "!{index}$2");
} else {
fieldName = fields[i].name.replace(/(\d+)(\D+)$/g, "!{index}$2");
fieldName = fieldName.replace(/(\d+)(\D+)$/g, "!{parentIndex}$2");
}
var j = Com_ArrayGetIndex(tbInfo.fieldNames, fieldName);
if(j>-1){
fieldName = tbInfo.fieldNames[j].replace(/!\{index\}/, index-tbInfo.firstIndex);
if (parentIndex != null || parentIndex != '')
fieldName = fieldName.replace(/!\{parentIndex\}/, parentIndex);
if(Com_Parameter.IE)
fields[i].outerHTML = fields[i].outerHTML.replace("name=" + fields[i].name, "name="+fieldName);
else
fields[i].name = fieldName;
} else {
//var _fn = fieldName;
var tbs = optTR.getElementsByTagName('TABLE');
if (tbs.length > 0 && tbs[0].getAttribute('KMSS_ParentIndex') != null) {
fieldName = fields[i].name.replace(/(\d+)(\D+)(\d+)(\D+)$/g, index-tbInfo.firstIndex + '$2$3$4');
//alert(fields[i].name + '\n' + _fn + '\n' + fieldName);
if(Com_Parameter.IE)
fields[i].outerHTML = fields[i].outerHTML.replace("name=" + fields[i].name, "name="+fieldName);
else
fields[i].name = fieldName;
}
}
}
}以上代码应该是用来替换一个中括号内的数字的,但是我看了好久都没看懂~求大神们帮我看看,加点注释,特别是有正则那几行~谢谢了
"!{index}$2"中$2是什么意思呀?
有人可以解释下吗?
对正则只是了解一点点,网上资料也不全,期待有人详解下上面的代码呀~~~
你的代码里是。
比如(\D+)匹配的是ABC。那么替换的时候$2的地方就是ABC
//其中fields[i].name="a[0].b[1].c"
//替换后变成"a[0].b[!{index}].c"
//还是不懂呀~正则里$不是表示结尾吗?为什么后面那个还要用$2?
fieldName = s.replace(/(\d+)(\D+)$/g, "!{index}$2");
对于这句,$1 匹配 1 ,而$2 匹配 "].c" 建议你还是系统的去学习一下js的正则吧.
$$ $ (JScript 5.5 或更新版本)
$& 指定与整个模式匹配的 stringObj 的部分。 (JScript 5.5 或更新版本)
$` 指定由 $& 描述的匹配之前的 stringObj 部分。 (JScript 5.5 或更新版本)
$' 指定由 $& 描述的匹配之后的 stringObj 部分。 (JScript 5.5 或更新版本)
$n 捕获的第 n 个子匹配,此处 n 为从1到9的十进制一位数。 (JScript 5.5 或更新版本)
$nn 捕获的第 nn 个子匹配,此处 nn 为从01到99的十进制两位数。 (JScript 5.5 或更新版本)
fieldName = fields[i].name.replace(/(\d+)(\D+)$/g, "!{index}$2");
alert(fieldName)//a[0].b[!{index}].c/*****************/
alert(fields[i].name)//a[0].b[1].c
fieldName = fields[i].name.replace(/(\d+)(\D+)$/g, "!{index}$1");
alert(fieldName)//a[0].b[1!{index}
现在我要修改~所以不可能是替换"].c"!!!!!!
我最欣赏13楼的回复,虽然可能是复制粘贴来的,但是解释得比较清楚全面。
而2楼和8楼,真希望你们不要那么浮躁,看清问题,自己测试一下再回答,我一开始就说是替换数字的,非要说是替换“ABC”和“].C”
你的结果肯定有错最后怎么可能会是a[0].b[1!{index}呢
应该是a[0].b[!{index}1你跟原来作者不同的是,人家上面是用的第二个匹配子串($2),你用的是第一个($1)$n是用来获取匹配子串的,n是从1开始的左括号即 "(" 从左到右的出的出现顺序。