Use js + regular expressions to add links to keywords _ javascript tips-js tutorial

Source: Internet
Author: User
This article mainly introduces how to use js + regular expressions to add links to keywords. It is very simple. If you need them, you can refer to the following link to add the disease names in an html Script to the disease database, add only once. Avoid hyperlinks or image links.

Str. replace ('diabetes ', 'diabetes') was initially used ');

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, we need to avoid links and tags,

Do not avoid other labels

:

The Code is as follows:


S = "first look at a diabetic medical record
"+
"Dr. Wang
"+
"Diabetes Overview
Diabetes incidence
Diabetic symptoms
"+
"";
Document. write (s );

A_reg = // 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 the two types of labels with {index}, process the remaining text, and then replace the label content.
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 + '}}');
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 ('
-------------------------
Step 2: replace the link with {index:
'+ S +'
');

S = s. replace (/diabetes/I, "Diabetes ");

Document. write ('
-------------------------
Step 2: add the link to the disease database:
'+ S +'
');

If (arr_ele ){
For (var I = 0; I s = s. replace ('{' + I + '}', arr_ele [I]);
}
}

Document. write ('
-------------------------
Step 2: replace the link with the following:
'+ S +'
');

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.