4 回答
TA贡献1963条经验 获得超6个赞
var str = "lajishuju..Ejhohaodf98 a.length+b.length+10+:rowspan=a.length + b.length+ c.length20:rowspan=a.length+b.length lajishujudsjalfj)Ufaojd";
var r = str.replace(/(:rowspan=)(.+?)(?=\d*:rowspan)/g, function (...m)
{
m[2] = m[2].replace(/\b([a-z]\w*\.length)/g, 'sub.$1');
return m[1] + m[2];
});
console.log(r);
TA贡献1799条经验 获得超6个赞
var str = "lajishuju..Ejhohaodf98 a.length+b.length+10+:rowspan=a.length + b.length+ c.length20:rowspan=a.length+b.length lajishujudsjalfj)Ufaojd";
let reg=/:rowspan=[^\d]*?(?:([a-z])\.length\s*\+\s*)+(?:([a-z])\.length)[^\d]*?\d+/g;
let result=str.replace(reg,$1=>$1.replace(/([a-z]\.)(?=length)/g,'sub.$1'));
console.log(result);
//lajishuju..Ejhohaodf98 a.length+b.length+10+:rowspan=sub.a.length + sub.b.length+ sub.c.length20:rowspan=a.length+b.length lajishujudsjalfj)Ufaojd
TA贡献1847条经验 获得超11个赞
str.replace(/:rowspan=([^\d]+)\d/g, function($1){return $1.replace(/(\w.length)/g, '^^.$1')})
至少需要2个正则 因为一个正则没办法在选定范围的情况下同时匹配里面随机个数的表达式
TA贡献1801条经验 获得超15个赞
var str = "lajishuju..Ejhohaodf98 a.length+b.length+10+:rowspan=a.length + b.length+ c.length20:rowspan=a.length+b.length lajishujudsjalfj)Ufaojd:rowspan=a.length + b.length2fsdfsdfsdf:rowspan=a.length + b.length2";
var gap = ':rowspan=';
var arrs = str.split(gap);
var head = arrs.shift();
var cnt = arrs.map(function(v){
if(/\d+/.test(v)){
var m = v.replace(/(\w*\.length)/g,'sub.$1');
return gap + m;
}else{
return gap + v;
}
});
var ret = head + cnt.join('');
添加回答
举报