RegExp on Search Results

This tutorial shows how to make use of Regular Expressions to make your search input return in a choosen style (bold, font, color) within the search result.

To make your keywords from a search form in a search result page bold you can use a RegExpression.

I this example i have a table enabled for FULLTEXT indexing on which i perform the search. The SQL statement for this looks approx. like this:

More on this subject please check this link.

Now i wanted to make the input keyword bold, so it's more clear to the viewer where his/her given keyword is located in the returned result.

I found an article on 4guysfromrolla where this was explained and made it suitable for use with my project.

As shown on the recordset above the parameter for the keyword is Request.Form("keyword") which contains the actual word that i wanted to make bold in the returned results.

Here is the RegExp to do this:

 

Some explaination:

Set regEx = creates the object
strKeyWord = create a string from the form result
regEx.Global = set true so it keeps searching after the first match (all)
regEx.IgnoreCase = set true for ignoring LCase en UCase
regEx.Pattern = the pattern to search, which is the keyword

Now that we have done that, we will give the column in the database where it will replace the keyword with a style (bold, font, color). This is the method that we are going to use.

Where you normally drag and drop the database field on the page you have to replace this with the following:

Some explaination:

regEx.Replace = this where we call the method to a certain set of data, in this case the column where the description (text) is.

Create a style for your RegExp and use $1 to give the location where the keyword has to be placed.

That's it, now you have a resultpage where the keyword input is made bold in the returned data which matches the keyword.

Note: this only works with one keyword, more keywords will still give a correct search result only the given input isn't made bold (example search: webdesign, ecommerce). I am working on it to make it work with more than one word. If someone has a idea how to do this, please share it with us.

Working example (results are in Dutch !)

Marcellino Bommezijn

Marcellino BommezijnMarcellino Bommezijn is one of the managers at dmxzone.com. He is a contributor on the tutorials section.

Owner of Senzes Media (http://www.activecontent.nl) which provides professional services and web applications for mid-sized companies.

ActiveContent CMS is the ASP.NET Content Management solution that is used for building professional and rich-featured websites.

See All Postings From Marcellino Bommezijn >>

Comments

Example link has changed !

September 28, 2001 by Marcellino Bommezijn

Please note that the link is under another domain now:

http://www.ultradev-asp.net/udzone_tutorials/faq/form.asp

Dynamic

April 23, 2002 by Rian Carstens

Thank you for the tutorial, is has been very helpful.  What I am trying to do is to create a dynamic regular expresion.  Basically I am have a table that holds names of people.  I want to create a dynamic search for all these people's names in an article page and link the names back to the persons detail page.

Here is an example for "Fidel":

"This is the body of the text where Fidel will be mentioned and wherever Fidel is mentioned it should be linked back to Fidel's biography page."

I do not want to have to hard code "fidel" in the script, since there is a huge list of changing names.

I can use this method of yours, but only if I specify the individual names in the script.  How can I specify that I need to search all the names in the table automaticaly?

Thank you in advance.

Rian

You must me logged in to write a comment.