Use js + regular expressions to add links for keywords and js Regular Expressions

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.