Click here to Skip to main content
13,141,157 members (63,535 online)
Click here to Skip to main content
Add your own
alternative version


41 bookmarked
Posted 15 Jun 2000

Keyword highlighting in ASP

, 15 Jun 2000
Rate this:
Please Sign up or sign in to vote.
A simple function to highlight keywords in ASP
<!-- Article image -->

Sample Image - Highlight.gif

<!-- Main HTML starts here -->


While working on the my version of the message board ASP scripts I have found myself in the need of a function that would highlight certain keywords within the text of the messages.

My first idea was to use VBScript's built-in Replace() function. The only problem with Replace was that I was not able to preserve the original case of the letters within the text. Consider following example:

myText = "Using Replace function to make word REPLACE bold."
myText = Replace(myText, "replace", "<b>replace</b>", 1, -1, 1)

The resulting string will be "Using <b>replace</b> function to make word <b>replace</b> bold.". It did make all the words "Replace" bold, but it also changed the case of these words from their original case.

This is why I had to write this little Highlight function. There is nothing special about this function, but it does what I needed it to do and I hope that you could use it somewhere in your ASP scripts as well. So here it is:

' HIGHLIGHT function will search text for a specific string
' When string is found it will be surrounded by supplied strings
' NOTE: Unfortunately Replace() function does not preserve the original case 
' of the found string. This function does.
' Parameters:
' strText 	- string to search in
' strFind	- string to look for
' strBefore	- string to insert before the strFind
' strAfter 	- string to insert after the strFind
' Example: 
' This will make all the instances of the word "the" bold
' Response.Write Highlight(strSomeText, "the", "<b>", "</b>")
Function Highlight(strText, strFind, strBefore, strAfter)
	Dim nPos
	Dim nLen
	Dim nLenAll
	nLen = Len(strFind)
	nLenAll = nLen + Len(strBefore) + Len(strAfter) + 1

	Highlight = strText

	If nLen > 0 And Len(Highlight) > 0 Then
		nPos = InStr(1, Highlight, strFind, 1)
		Do While nPos > 0
			Highlight = Left(Highlight, nPos - 1) & _
				strBefore & Mid(Highlight, nPos, nLen) & strAfter & _
				Mid(Highlight, nPos + nLen)

			nPos = InStr(nPos + nLenAll, Highlight, strFind, 1)
	End If
End Function

To use this function - include it in your ASP scripts and call it like this:

Response.Write Highlight(myText, "someword", "<font color=red>", "</font>")


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Konstantin Vasserman
Web Developer
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionMessage Closed Pin
18-Oct-16 21:12
memberer.vks18-Oct-16 21:12 
Questionsummrizing the result [modified] Pin
feras.a30-May-06 3:03
memberferas.a30-May-06 3:03 
QuestionReplace in HTML elements Pin
arbpen4-Feb-06 6:11
memberarbpen4-Feb-06 6:11 
GeneralHighlighting and Anchoring Pin
Anonymous26-Jul-04 10:17
sussAnonymous26-Jul-04 10:17 
QuestionHow to highlight Multiple Keywords using this Function? Pin
TommyX17-Jun-03 16:40
sussTommyX17-Jun-03 16:40 
AnswerRe: How to highlight Multiple Keywords using this Function? Pin
Anonymous23-Sep-03 7:17
sussAnonymous23-Sep-03 7:17 
AnswerRe: How to highlight Multiple Keywords using this Function? Pin
Anonymous8-Jan-04 18:08
sussAnonymous8-Jan-04 18:08 
GeneralHighlighting Options in a combo box Pin
Anonymous20-May-02 4:45
memberAnonymous20-May-02 4:45 
Questionwhy not regular expression? Pin
internal6-Feb-02 19:55
memberinternal6-Feb-02 19:55 
AnswerRe: why not regular expression? Pin
msraot5-Jul-04 6:16
membermsraot5-Jul-04 6:16 
GeneralRe: why not regular expression? Pin
internal6-Jul-04 16:54
memberinternal6-Jul-04 16:54 
Questionkeyword in HTML tags ? Pin
Thomas Parvais2-Feb-01 11:25
memberThomas Parvais2-Feb-01 11:25 
AnswerRe: keyword in HTML tags ? Pin
Konstantin Vasserman2-Feb-01 11:36
memberKonstantin Vasserman2-Feb-01 11:36 
GeneralRe: keyword in HTML tags ? Pin
ModemMike15-Apr-02 17:50
memberModemMike15-Apr-02 17:50 
GeneralRe: keyword in HTML tags ? Pin
Konstantin Vasserman17-Apr-02 18:10
memberKonstantin Vasserman17-Apr-02 18:10 
AnswerRe: keyword in HTML tags ? Pin
julianjordan4-Jan-17 1:08
memberjulianjordan4-Jan-17 1:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170915.1 | Last Updated 16 Jun 2000
Article Copyright 2000 by Konstantin Vasserman
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid