Home >Web Front-end >JS Tutorial >Use js regular expressions to add links to keywords_javascript skills
It is required to add the disease name in an HTML script to the link of the disease database, only add it once, and avoid hyperlinks or image links.
Initially used str.replace('diabetes', 'diabetes');
After searching for a long time, I couldn’t find the replacement effect. It turned out that there was a picture whose title contained diabetes, and it was posted first.
Therefore, the link and tags should be avoided, but tags such as Above picture:
s = "Look at a diabetes medical record first
"
"Dr. Wang of Diabetes
"
"Introduction to diabetes
Incidence of diabetes
Symptoms of diabetes
"
" ";
document.write(s);
a_reg = //i; //Regular for a link
img_reg = //i; //Regulations for image links to prevent the image’s title, alt and other attributes from including disease names from being mistakenly replaced
var ix = 0;
var arr_ele = [];
//First replace all the 2 type tags with {{index}}, then process the remaining text, and then replace the content of the tag back
while(true){
If(-1 == s.toLowerCase().indexOf('
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 '}}');
}
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 '}}');
}
console.log(s);
}
document.write('
-------------------------
Step 1: Replace the link with {{index} After }:
' s '
');
s = s.replace(/diabetes/i, "diabetes");
document.write('
-------------------------
Step 2: After adding the disease database link:< br>' s '
');
if(arr_ele){
for(var i=0; i
}
}
document.write('
-------------------------
Step 3: After replacing the link back:< br>' s '
');