87,989
社区成员
发帖
与我相关
我的任务
分享
srt.replace(/蛤蛤/g,<a href="https://www.baidu.com">蛤蛤</a>)
var str = '我是字符串,蛤蛤,我是字符串,蛤蛤<a href="https://www.baidu.com">我是链接</a>蛤蛤';
var replaceIndex = [1], i = 0;
str = str.replace(/蛤蛤/ig, v => {
if(replaceIndex.indexOf(i++) >= 0){
return `<a href="https://www.baidu.com">${v}</a>`;
}
return v;
});
console.log(str);
var str = '我是字符串,蛤蛤,我是字符串,蛤蛤<a href="https://www.baidu.com">我是链接</a>蛤蛤';
var seq = 0;
var result = str.replace(/蛤蛤/g,function(match,index,orgStr){
seq++;
if(seq === 2){
return '<a href="https://www.baidu.com">蛤蛤</a>';
}else{
return match;
}
});
console.log(result);