Use js + regular expressions to add links for keywords and js Regular Expressions
You must add the disease name in an html Script to the disease database only once. Avoid hyperlinks or image links.
Str. replace ('diabetes ',' <a href =...> diabetes </a> ');
After searching for the results for half a day, the effect of replacement was not found. It turned out that there was an image title containing diabetes, and it was first posted.
Therefore, the <a> link and labels should be avoided, but <p> <div> and other labels should not be avoided.
:
Copy codeThe Code is as follows:
S = "<a href = 'HTTP: // www.yx129.com/bingli/1_310.html'> first view a diabetic medical record </a> <br/>" +
" Dr. Wang <br/>" +
"Diabetes introduction <br/> incidence of diabetes <br/> <a href = 'Baidu. com '> diabetic symptoms <br/>" +
"</A> ";
Document. write (s );
A_reg =/<(.*?) <\/A>/I; // The regular expression of the link
Img_reg =/ /I; // The regular expression of the image link to prevent incorrect replacement of the title and alt attributes of the image, including the disease name
Var ix = 0;
Var arr_ele = [];
// Replace all <a> 2 tags with {index}, and then process the remaining text, replace the content of the <a> label
While (true ){
If (-1 = s. toLowerCase (). indexOf ('<A') &-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 + '}}');
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> ----------------------- <br> Step 2: replace the link with {index}: <br>' + s + '<br> ');
S = s. replace (/diabetes/I, "<a target = '_ blank' href = 'HTTP: // jibing.yx129.com/tnb'> diabetes </a> ");
Document. write ('<br> ----------------------- <br> Step 2: add the link to the disease Database: <br>' + s + '<br> ');
If (arr_ele ){
For (var I = 0; I <arr_ele.length; I ++ ){
S = s. replace ('{{' + I +'} ', arr_ele [I]);
}
}
Document. write ('<br> ----------------------- <br> Step 2: After the link is replaced: <br>' + s + '<br> ');
The above is all the code that uses js + regular expressions to add links to keywords. For more information, see