Forum Moderators: open

Message Too Old, No Replies

Regular expression in ASP

         

musicales

5:56 pm on Mar 1, 2004 (gmt 0)

10+ Year Member



Can anyone give me the ASP code to do the following:

replace all occurences of a work with their bold equivalent, irrespective of case so a lower case search for "donnie darko"

would fine and replace "Donnie Darko" with its bold equivalent, as well as "DONNIE DARKO" etc.

Many thanks for your help.

Xoc

9:05 pm on Mar 3, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here is the solution in standard ASP, with some test code. The function Bold takes two parameters: the input string and the string to find and bold:

<%
Function Bold(strInput, strSearch)
Dim istart
Dim iend
Dim icur
Dim strOutput
dim intLen

strOutput = strInput
intLen = Len(strSearch)

icur = 1
istart = InStr(icur, strOutput, strSearch, 1)
Do While istart
strOutput = Left(strOutput, istart - 1) & "<b>" & Mid(strOutput, istart, intLen) & "</b>" _
& Mid(strOutput, istart + intLen)
icur = istart + intLen + 7
istart = InStr(icur, strOutput, strSearch, 1)
Loop

Bold = strOutput
End Function

Dim strInput

strInput = "would find and replace Donnie Darkowith its "
strInput = strInput & "bold equivalent, as well as DONNIE DARKOWITH etc."

Call Response.Write("Before: " & strInput & "<br />")
Call Response.Write("After: " & Bold(strInput, "Donnie Darkowith"))
%>

Xoc

10:28 pm on Mar 3, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Furthermore, just to show how you could do this in .NET (examples in Visual Basic .NET)...

If you wanted to allow only case sensitive replacements, you could just do:


strInput.Replace(strSearch, "<b>" & strSearch & "</b>")

But to do case insensitive replacements, you need to use a Regular Expression shared method:

Regex.Replace(strInput, strSearch, "<b>$&</b>", RegexOptions.IgnoreCase)

Regex is in the System.Text.RegularExpressions namespace.

This expression says find strSearch in strInput. Then replace it with <b>the search string</b>. The $& is the regular expression code to say the search string. The option RegexOptions.IgnoreCase says to do a case insensitive comparison.

The regular expression parser in the .NET framework is incredibly powerful.

musicales

11:03 pm on Mar 3, 2004 (gmt 0)

10+ Year Member



Many thanks that's exactly what I was after.

ziggystardust

9:11 am on Mar 4, 2004 (gmt 0)

10+ Year Member



Hello.

Actually, classic Asp already has a function that works more or less like String.Replace in VB.NET

This invaluable function is called Replace and in it's most basic form it takes three parameters:

plainString = "Donnie Darko bla bla bla donnie darko"

boldString = Replace(plainString, "Donnie Darko", "<b>Donnie Darko</b>")

This changes all occurrences of Donnie Darko in the string plainString to <b>Donnie Darko</b> and stores the new string as boldString.

Unfortunately, this is case sensitive... but we can solve that by using the optional parameters supplied in the Replace function:

plainString = "Donnie Darko bla bla bla donnie darko"

boldString = Replace(plainString, "Donnie Darko", "<b>Donnie Darko</b>", 1, -1, vbTextCompare)

Voilá, one line of code.

The first 1 is the Start parameter. It specifies what position (from left) in the string you want the search to start from. In this case, we don't want to skip anything, so the function will start from the first character.

The second parameter (-1 here) is the Count parameter and specifies how many times to replace the substring. The value -1 means the function will replace ALL occurrences in the string. If, for some reason, you only want to replace the first 4 "Donnie Darko" with "<b>Donnie Darko</b", you set this to 4 etc.

The last parameter is the Compare parameter. The default value for this is vbBinaryCompare meaning the replace will be case sensitive. If we want the function to ignore case, we have to use vbTextCompare instead.

Hope this helped.
//ZS

Xoc

9:22 am on Mar 4, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That's nice. I didn't remember VBScript implementing the Replace function.

Except for one thing. It will wind up changing DONNIE DARKO or any other capitalization of the name to Donnie Darko. The code I gave retains the original capitalization.

ziggystardust

9:36 am on Mar 4, 2004 (gmt 0)

10+ Year Member



You're right... I feel stupid for missing that :)

In that case, I'd go with something like the regular expression Xoc posted.

//ZS

musicales

9:54 am on Mar 4, 2004 (gmt 0)

10+ Year Member



ziggystardust - I was aware of the vbTextCompare parameter but when I tried it it didn't seem to work in terms of affecting the case sensitivity.

Xoc

10:10 am on Mar 4, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In VBScript, the constant isn't defined automatically as it is in VB. You actually have to pass the constant's value or define the constant to contain that value, as in

Const vbTextCompare = 1

ziggystardust

11:50 am on Mar 4, 2004 (gmt 0)

10+ Year Member



Musicales - how did your function call look like? You have to pass in the Start and Count parameters as well for it to work.

Xoc - I'm pretty certain the constant is defined automatically in VBScript... Try creating a simple webpage:

<%
Const vbTextCompare = 1
%>

It'll give you a "Name redefined" error.

//ZS

musicales

12:44 pm on Mar 4, 2004 (gmt 0)

10+ Year Member



ziggystardust - I may have missed the others and just used replace(a,"<B>" & a & "</b>,1)
I will try the full version you suggest.

Xoc

5:14 pm on Mar 4, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ack, you are right. It is only the ADO constants that aren't defined.