var dstr='<p id="para"> demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo<a href="#table1(t1)" id="ytD2F">table1</a> test demo test demo test demo test demo test demo <a href="#table2(t2)" id="ytD2F">table2</a> demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test </p>';
我必须找到所有href属性,其中&#34; table&#34;字符串应该存在,例如:href =&#34;#table1(t1)&#34;然后我必须用括号中的字符串替换它,例如:href =&#34; t1&#34;
答案 0 :(得分:1)
希望有所帮助:
var href1=document.getElementsByTagName('a');
for(i=0;i<href1.length;i++) {
var val=href1[i].getAttribute('href');
if(val!=null && val.indexOf("table")>=0) {
var str=val.substring(val.lastIndexOf('(')+1,val.lastIndexOf(')'));
href[i].setAttribute('href',str);
}
}
答案 1 :(得分:1)
这是一种方法:
$init = $('a[href*="table"]').attr('href');
$('a[href*="table"]').attr('href',$init.substring($init.lastIndexOf('(')+1,$init.lastIndexOf(')')));
jsfiddle:https://jsfiddle.net/vxpyyy15/3/
答案 2 :(得分:0)
如果我理解正确,你需要这个:
var re = /(#table\d+)|\((\w{1,2})(\))/g;
var str = '<p id="para"> demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo<a href="#table1(t1)" id="ytD2F">table1</a> test demo test demo test demo test demo test demo <a href="#table2(t2)" id="ytD2F">table2</a> demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test </p>';
var subst = '$2';
var result = str.replace(re, subst);
console.log(result);
Regex101演示:https://regex101.com/r/uP6uY7/1