我想分割字符串的特定部分,并用新值替换它。注册前分割并替换字符串javascript/jquery
实施例:var _string = "split and replace @123/0 and test @456/1 and so on..."
从上面的字符串我需要@ 123.00/0来替换@ 123/0和456 @/0 @ 456.00/0。
最终输出:"split and replace @123.00/0 and test @456.00/1 and so on..."
我正在寻找一个更通用的方法,因为有可能是@ val的n个/在一个字符串。我无法替换字符串的特定部分。
这是我的尝试:
var _string = "split and replace @123/0 and test @456/1 and so on...";
var regex = /\$[^\@]*\/0/g;
var match = _string.match(regex);
for(var i=0; i<match.length; i++){
if(match[i].indexOf("@") > 0){
// do replace of string...
}
}
由于有机磷农药提到这个需要是通用的,我相信整个的一点是,替代值不能硬编码 –