English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

JS batch replacement of keywords in content to hyperlinks

People who understand a bit of SEO know that you need to add links to keywords in the content to form on-site anchor text links, which is very helpful for SEO.

The idea is to enter a number of keywords and the corresponding links of keywords in the database, of course, the links can be automatically generated according to the keyword id, or directly use the keyword as the link parameter, such as?tag=1?kw=Keywords.

This problem is not as simple as a simple batch replace, it needs to take into account the existing hyperlinks, and the text inside cannot be replaced again, as well as the alt attribute of the image, or the title attribute of other tags, the text inside should not be replaced either.

See the following HTML code:

【<a href="http://www.baidu.com" rel="external nofollow" rel="external nofollow"/Jiuzhaigou is located in Jiuzhaigou County, Aba Prefecture, Sichuan Province.5A scenic spot, a world natural heritage, with the scenery of "Five Wonders", the most incredible to me is the sea lake, a must-visit scenic area in this life. Suitable for visiting all year round, autumn is the most beautiful.
<img src="" alt="" title=""}} />

In this case, you can't directly replace Jiuzhaigou with a hyperlink, otherwise the first link will be replaced with a hyperlink containing a hyperlink, and then the alt and title on the image will also be replaced with a hyperlink, which is definitely not in line with HTML standards.

Scenario one: Exclude keywords in attributes

The matching regular expression is: Keywords[^<]*>), so the regular expression to exclude this keyword is: Keywords?!([^<*)).

Scenario two: Exclude keywords in links

The matching regular expression is: Keywords[^<]*<\/a>, so the regular expression to exclude this keyword is: Keywords?!([^<*<\/a>)

The result of integrating both cases is: var reg=/Keywords(?!([^<*>)|([^<*<\/a>))/ig;

There is no need to talk nonsense, let's give a complete batch replacement example:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<div id="content">
【<a href="http://www.baidu.com" rel="external nofollow" rel="external nofollow"/Jiuzhaigou is located in Jiuzhaigou County, Aba Prefecture, Sichuan Province.5A scenic spot, a world natural heritage, with the scenery of "Five Wonders", the most incredible to me is the sea lake, a must-visit scenic area in this life. Suitable for visiting all year round, autumn is the most beautiful.
<img src="" alt="" title=""}} />
</div>
<hr />
<div id="new">
</div>
<script>
var c=document.getElementById("content").innerHTML;
//var reg=/Jiuzhaigou(?!([^<*>)|([^<*<\/a>))/ig;
var json=[
  {'key':'Jiuzhaigou','url':'/Jiuzhaigou/}
  ,{'key':'Scenic spot','url':'/Scenic spot/}
  ,{'key':'Scenic spot','url':'/Scenic spot/}
 ];
var reg;
for(var i=0;i<json.length;i++{
 var j=json[i];
 reg=new RegExp(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:

That's all for this article. I hope the content of this article can bring you some help in learning or work, and I also hope to get more support for the Yelling Tutorial!

Statement: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, does not edit the content manually, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please feel free to send an email to: notice#w3Please report any infringement by sending an email to notice#w (replace # with @ when sending an email), and provide relevant evidence. Once verified, this site will immediately delete the suspected infringing content.

You may also like