Forum Moderators: open
<p id="sometext">so, prasanth, what do you think of Webmasterworld? best place on the web?</p>
<a href="#" onClick="highlight('sometext'); return false;">highlight</a>
<script type="text/javascript">
function highlight(el) {
var theText = document.getElementById(el).innerHTML;
theText = theText.replace(/(web)/gi, "<b>$1</b>");
document.getElementById(el).innerHTML = theText;
}
</script>
All occurences of the substring 'web' in the element 'sometext' will be marked with <b>.
<html>
<head>
<script language="JavaScript">
function display() {
var contentString =content.innerHTML;
var mainString = location.href;
alert(mainString);
var choice =location.href.split("?")[1].split("=")[1];
var finalStr= unescape(choice).split(" ");
var expr=finalStr[0];
for(i=1;i<finalStr.length;i++)
{
expr = expr+"¦"+finalStr[i];
alert(expr);
}
var rg=new RegExp(expr,"gi");
var endresult = contentString.replace(rg,function(thematch){ return '<span style="color:black;background-color:yellow">'+thematch+'</span>' ; } );
document.write(endresult);
if(contentString.search(finalStr)!=-1)
{
document.write(endresult);
}
else
{
alert("string not found");
}
}
</script>
</head>
<body onLoad="display()" >
<div id="content">
This is sample page to test the regular expression using javascript
What is a cookie?
A cookie is a chunk of information that you're allowed to save on a user's computer in a structured and somewhat limited fashion. The official cookie specification is available at Netscape, and specifies the format that defines a cookie. Basically, a cookie consists of a 'name=value' pair and several optional attributes which we'll discuss below.
The optional attributes allow you more control over your cookies. Using them can help you determine how long the cookie is valid and what hosts within a domain may have access to them. You can also specify a path, so that any scripts in it or subdirectories below it can read or write the cookie. And you can specify whether a cookie will be sent over a secure communications channel.
So you see, cookies can be a very useful addition to your web site. Let's look at them in more detail.
</div>
</body>
</html>
HTH!
<p id="sometext">so, prasanth, what do you think of Webmasterworld? best place on the web?</p>
<a href="#" onClick="highlight('sometext'); return false;">highlight</a>
<script type="text/javascript">
function highlight(el) {
var allTheWords = document.getElementById(el).innerHTML.split(' ');
var myWords = "a do web".split(" ");
for (var i = 0; i < myWords.length; i++) {
myWords[i] = "(^" + myWords[i] + "\\W?$)";
}
myWords = myWords.join("¦"); // has to be a solid pipe!
var dynaPat = new RegExp(myWords, "gi");
for (var i = 0; i < allTheWords.length; i++) {
if (dynaPat.test(allTheWords[i])) {
allTheWords[i] = "<strong>" + allTheWords[i] + "</strong>";
}
}
document.getElementById(el).innerHTML = allTheWords.join(' ');
}
</script>
Not very elegant, because it loops through all the words in the element, but it seems to work, more or less.
Adapt var myWords to read the query string (location.search).