<!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>
<script>
str = "This is an apple and I love it.";
function searching(query){
var patt = new RegExp(query,"gi");
var query = "<span style='color: red'>"+query+"</span>";
document.getElementById("text").innerHTML = str.replace(patt,query);
return str = document.getElementById("text").innerHTML;
}
/*
patt1 = new RegExp("is","gi");
patt2 = /\san\s/gi;
document.write(str.replace(patt2,"<span style='color:red'> are </span>"));
*/
</script>
</head>
<body>
<div id="text"><script>document.write(str)</script></div>
<form>
<input type="text" id="querys" value="" onkeyup="searching(this.value)" />
</form>
</body>
</html>
I want after each search the result replaces the original string with the hightlight (red). For example, For the first time, I search "apple". "apple" is highlighted in the string. Return the string in the div with the red highlight. For the second time, I search "is" "is" is highlighted with red in the string.
I tried to use innerHTML but innerHTML will also return the inline CSS style tags.