The code copy is as follows:
<!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>Keyword Highlight</title>
</head>
<body>
<div id="textbox">
<p>Baidu (Nasdaq BIDU) is the world's largest Chinese search engine. It was founded in Zhongguancun, Beijing by Robin Li and Xu Yong in January 2000. It is committed to providing people with "simple and dependable"</p>
<p>Information acquisition method. The word "Baidu" originated from the lyrics of "Qingyu Case·Yuanxi" by Xin Qiji, a poet of the Song Dynasty in China, "Looking for him thousands of miles from the crowd", symbolizing Baidu's persistent pursuit of Chinese information retrieval technology. </p>
</div>
<script>
function highlight(idVal, keyword) {
var textbox = document.getElementById(idVal);
if ("" == keyword) return;
//Get all text content
var temp = textbox.innerHTML;
console.log(temp);
var htmlReg = new RegExp("/<.*?/>", "i");
var arr = new Array();
//Replace HTML tag
for (var i = 0; true; i++) {
//Match html tag
var tag = htmlReg.exec(temp);
if (tag) {
arr[i] = tag;
} else {
break;
}
temp = temp.replace(tag, "{[(" + i + ")]}");
}
// Talking about keyword splitting and incorporating into arrays
words = decodeURIComponent(keyword.replace(//,/g, ' ')).split(//s+/);
//Replace keyword
for (w = 0; w < words.length; w++) {
// Match keywords and retain special characters that can appear in the keywords
var r = new RegExp("(" + words[w].replace(/[(){}.+*?^$|///[/]]/g, "//$&") + ") ", "ig");
temp = temp.replace(r, "<b style='color:Red;'>$1</b>");
}
//Restore HTML tags
for (var i = 0; i < arr.length; i++) {
temp = temp.replace("{[(" + i + ")]}", arr[i]);
}
textbox.innerHTML = temp;
}
highlight("textbox","Baidu, Robin Li");
</script>
</body>
</html>