Know a bit SEO people know to give the content of keywords plus links, the formation of the anchor text link in the station, this SEO has a lot of help.
The idea is to enter a number of keywords and keywords corresponding links in the database, of course, the link can be automatically generated according to the ID of the keyword, or directly using keywords as a link parameter, such as tag=1, kw= keyword.
This problem is not simple a batch replace so simple, to consider the existing hyperlinks, you can not replace the text inside the hyperlink again, there is the alt attribute of the picture, or the title property of other tags, the text inside should not be replaced.
See the following HTML code:
"<href=" http://www.baidu.com "> Jiuzhaigou Overview </a > Jiuzhaigou is located in Sichuan ABA Jiuzhaigou County Territory, 5A attractions, the world's natural heritage, there is "five must" scenery, I feel the most absolute is haizi, this life can not go to the scenic area. All year round, autumn is the most beautiful. <src= "" alt= "Jiuzhaigou pictures" title = "Jiuzhaigou Pictures" />
In this case you can not directly replace Jiuzhaigou as a hyperlink Ah, otherwise the first link will be replaced by the link contains links, and then the image of Alt and title also replaced a link, which is certainly not in line with the HTML specification.
Scenario One: Exclude keywords from attributes
Match the regular is: keywords [^<]*>, so the rule to exclude this keyword is: keywords?! ([^<]*>).
Scenario Two: Exclude keywords from links
Match the regular is: keywords [^<]*<\/a>, so the rule to exclude this keyword is: keywords?! ([^<]*<\/a>).
The result of integrating the two cases is: var reg=/keywords (?! ([^<]*>) | ([^<]*<\/a>)) /ig;
Needless to say, a complete batch replacement example is given at the end:
<!DOCTYPE HTML Public "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.w3.org/TR/xhtml1/DTD/ Xhtml1-transitional.dtd "><HTMLxmlns= "http://www.w3.org/1999/xhtml"><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8" /><title>Untitled Document</title></Head><Body><DivID= "Content">"<ahref= "Http://www.baidu.com">Jiuzhaigou Overview</a>Jiuzhaigou is located in Sichuan ABA Jiuzhaigou County Territory, 5A attractions, the world's natural heritage, there is "five must" scenery, I feel the most absolute is haizi, this life can not go to the scenic area. All year round, autumn is the most beautiful. <imgsrc=""alt= "Jiuzhaigou Pictures"title= "Jiuzhaigou Pictures" /></Div><HR/><DivID= "new"></Div><Script>varC=document.getElementById ("content"). InnerHTML;//var reg=/jiuzhaigou (?!) ([^<]*>) | ([^<]*<\/a>)) /ig;varJSON=[ {'Key':'Jiuzhaigou','URL':'/Jiuzhaigou/'} ,{'Key':'Attractions','URL':'/Attractions/'} ,{'Key':'Scenic Area','URL':'/Scenic/'} ];varreg; for(varI=0; I<Json.length;i++){ varJ=Json[i]; Reg=NewRegExp (J.key+"(?! ([^<]*>) | ([^<]*<\/a>))","IG"); C=C.replace (Reg,"<a href= '"+J.url+"' >"+J.key+"</a>");} document.getElementById ("New"). InnerHTML=C;</Script></Body></HTML>
The effect after replacement:
JS Bulk replacement content in the keywords are hyperlinks, avoid existing links and Alt, title in the keywords