It is required to add the disease name in an html script to the link to the disease library, only once, avoid hyperlinks or image links.
Originally used str.replace('diabetes', '<a href=...>diabetes</a>');
After searching for a long time, I couldn't find the effect after the replacement. It turned out that there was a picture title containing diabetes, and it was the first to log in.
Therefore, you should avoid <a> links and <img> tags, but you should not avoid tags such as <p><div>.
Above:
The code copy is as follows:
s = "<a href='http://www.yx129.com/bingli/1_310.html'>Look at a diabetes record first</a> <br/>" +
"<IMG style='vertical-align:middle' width=40 src='http://yx129.com/api/minisite/images/skin/green/doctor_thumb_100.png'/>Doctor Wang of diabetes<br/>" +
"Introduction to Diabetes<br/>Diabetes Incidence<br/><a href='baidu.com'>Diabetes Symptoms<br/>" +
"</a> ";
document.write(s);
a_reg = /<a(.*?)<//a>/i; // the regularity of the link
img_reg = /<img(.*?)>/i; //The regularity of the image link prevents the image title, alt and other attributes including disease names and are replaced by mistake
var ix = 0;
var arr_ele = [];
//First replace all the <a><img>2 class tags with {{index}}, then process the remaining text, and then replace the content of the <a><img> tag back
while(true){
if(-1 == s.toLowerCase().indexOf('<a ') && -1 == s.toLowerCase().indexOf('<img ')){
break;
}
a_match = s.match(a_reg);
if(a_match){
//console.log(a_match);
arr_ele.push(a_match[0]);
s = s.replace(a_reg, '{{' +ix+ '}}');
ix++;
}
img_match = s.match(img_reg);
if(img_match){
//console.log(img_match);
arr_ele.push(img_match[0]);
s = s.replace(img_reg, '{{' +ix+ '}}');
ix++;
}
console.log(s);
}
document.write('<br>-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
s = s.replace(/diabetes/i, "<a target='_blank' href='http://jibing.yx129.com/tnb'>diabetes</a>");
document.write('<br>-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
if(arr_ele){
for(var i=0; i<arr_ele.length; i++){
s = s.replace('{{' + i + '}}', arr_ele[i]);
}
}
document.write('<br>-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
The above is all the code to add links to keywords using js+regex. It's simple. If you need it, please refer to it.