gpt4 book ai didi

javascript - 使用javascript在括号内添加字符串

转载 作者:行者123 更新时间:2023-12-03 07:53:42 24 4
gpt4 key购买 nike

“AddMoreRowsWithNo('SnapIII_btnEquipmentAddMore', 'SnapIII_TBLAditionalEquipment', 'SnapIII_trAdditionalEquipment', 'SnapIII_tdEquipmentNo')”

我需要在字符串内的右括号之前添加值。例如,

AddMoreRowsWithNo('SnapIII_btnEquipmentAddMore', 'SnapIII_TBLAditionalEquipment', 'SnapIII_trAdditionalEquipment', 'SnapIII_tdEquipmentNo', valCount);

最佳答案

这是带有正则表达式和替换函数的函数,第一次将传递 func 的名称作为变量,第二次 - 你的 func 参数

 function addParams(funcStr, paramsArrayToAdd){
var isFirstCall = false;
return funcStr.replace(/[^\)\(]+/g,function(str){
if(!isFirstCall){
isFirstCall = true;
return str;
}else{
return str.length > 0 ? str + "," + paramsArrayToAdd.join(",") : paramsArrayToAdd.join(",");
}
});
}

使用示例

    var str = addParams("AddMoreRowsWithNo('SnapIII_btnEquipmentAddMore', 'SnapIII_TBLAdditionalEquipment', 'SnapIII_trAdditionalEquipment', 'SnapIII_tdEquipmentNo')",["'param1'","'param2'"])
console.log(str);//AddMoreRowsWithNo('SnapIII_btnEquipmentAddMore', 'SnapIII_TBLAdditionalEquipment', 'SnapIII_trAdditionalEquipment', 'SnapIII_tdEquipmentNo','param1','param2')

关于javascript - 使用javascript在括号内添加字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34894276/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com