Replace in ASP/VBS/SQL

P

Pret Orian

I need to replace all occurancies of "is" in "This is a test (is), is,
penis." with "<a href=is.htm>is</a>".
The thing is, if I use a simple Replace function, the "is" in penis
also gets replaced.
I need to do it either in ASP/VBS or MSSQL, but I'd have it rather
done in ASP.
TIA
 
M

Michael Harris \(MVP\)

Pret said:
I need to replace all occurancies of "is" in "This is a test (is), is,
penis." with "<a href=is.htm>is</a>".
The thing is, if I use a simple Replace function, the "is" in penis
also gets replaced.
I need to do it either in ASP/VBS or MSSQL, but I'd have it rather
done in ASP.
TIA


s1 = "This is his test (is), is, is."
with new regexp
.global = true
.ignorecase = true
.pattern = "\bis\b"
s2 = .replace(s1,"<a href=is.htm>is</a>")
end with

msgbox s1 & vbcrlf & s2


--
Michael Harris
Microsoft.MVP.Scripting
Seattle WA US

Technet Script Center
http://www.microsoft.com/technet/scriptcenter/default.asp

Microsoft® Windows®2000 Scripting Guide
http://www.microsoft.com/technet/scriptcenter/scrguide/sagsas_overview.asp
 
P

Pret Orian

I need to replace all occurancies of "is" in "This is a test (is), is,
penis." with "<a href=is.htm>is</a>".
The thing is, if I use a simple Replace function, the "is" in penis
also gets replaced.
I need to do it either in ASP/VBS or MSSQL, but I'd have it rather
done in ASP.
TIA

I'm afraid I didn't explain my problem god enough.
I want to replace "is" only if it is not a part of another word, so
something like [^a-zA-Z]is[^a-zA-Z] pattern. "(is)", "is," should all
be positive matches, and "penis" should not.
 
J

Joe Earnest

Pret Orian said:
I'm afraid I didn't explain my problem god enough.
I want to replace "is" only if it is not a part of another word, so
something like [^a-zA-Z]is[^a-zA-Z] pattern. "(is)", "is," should all
be positive matches, and "penis" should not.

Did you try Michael Harris' script? (He politely uses "his" in his example
to demonstrate.)

Joe Earnest
 
P

Pret Orian

Did you try Michael Harris' script? (He politely uses "his" in his
example
to demonstrate.)

I did try the Michael's script and it did work indeed (thanx Michael).
However, I'm having a problem with "is" word standing on the beginning
of the string (or at the end). What would be a pattern that would
exclude all the characters that are not a-zA-Z except if beeing on the
beginning of the string
For example:

..pattern = "\bis\b"

Replaces: "this is something" "(is)" " This is." ...
But leaves "is this something" untouched.

Is there a way out?
Where are the escape characters (like \b) documented?
regards
 
J

Joe Earnest

Hi,

Pret Orian said:
I did try the Michael's script and it did work indeed (thanx Michael).
However, I'm having a problem with "is" word standing on the beginning
of the string (or at the end). What would be a pattern that would
exclude all the characters that are not a-zA-Z except if beeing on the
beginning of the string
For example:

.pattern = "\bis\b"

Replaces: "this is something" "(is)" " This is." ...
But leaves "is this something" untouched.

Is there a way out?
Where are the escape characters (like \b) documented?
regards

I'm not much at regex, and maybe some one can post a simple regex addition,
but as a fallback, you could do the beginning and end issues with standard
string manipulation, after using Michael's regex script. It's kludgy, but
it works.

s1= "is this something? it is, it is"

' Michael's regex script
with new regexp
.global = true
.ignorecase = true
.pattern = "\bis\b"
s2 = .replace(s1,"<a href=is.htm>is</a>")
end with

'add
if lcase(left(s2 & " ", 3)="is ") then _
s2= "<a href=is.htm>is</a>" & mid(s2, 3)
if lcase(right(s2, 3)=" is") then _
s2= left(s2, len(s2) -2) & "<a href=is.htm>is</a>"

msgbox s1 & vbcrlf & s2

Joe Earnest
 
M

Michael Harris \(MVP\)

Pret said:
I did try the Michael's script and it did work indeed (thanx Michael).
However, I'm having a problem with "is" word standing on the beginning
of the string (or at the end). What would be a pattern that would
exclude all the characters that are not a-zA-Z except if beeing on the
beginning of the string

s1 = "Is this what his is_test is? - Is IS iS - yes, it is"
with new regexp
.global = true
.ignorecase = true
.pattern = "\W(is)\W"
s2 = .replace(s1,"<a href=is.htm>is</a>")
end with

msgbox s1 & vbcrlf & s2

--
Michael Harris
Microsoft.MVP.Scripting
Seattle WA US

Technet Script Center
http://www.microsoft.com/technet/scriptcenter/default.asp

Microsoft® Windows®2000 Scripting Guide
http://www.microsoft.com/technet/scriptcenter/scrguide/sagsas_overview.asp
 
C

Chris Hohmann

Michael Harris (MVP) said:
What I actually meant was this...notice the $1 in the replacement string to
use the text that was matched in the replacement text...

s1 = "Is this what his is_test is? - Is IS iS - yes, it is"
with new regexp
.global = true
.ignorecase = true
.pattern = "\W(is)\W"
s2 = .replace(s1,"<a href=is.htm>$1</a>")
end with

msgbox s1 & vbcrlf & s2

--
Michael Harris
Microsoft.MVP.Scripting
Seattle WA US

Technet Script Center
http://www.microsoft.com/technet/scriptcenter/default.asp

Microsoft® Windows®2000 Scripting Guide
http://www.microsoft.com/technet/scriptcenter/scrguide/sagsas_overview.asp

Michael, your code consumes the surrounding "non-word" characters.
Here's some code based on the OP's original pattern
"[^a-zA-Z]is[^a-zA-Z]". I reduced the range since ignore case was in
effect.

<script language="VBScript" runat="SERVER">
str = "Is this what his is_test is? - Is IS iS - yes, it is"
with new regexp
.global = true
.ignorecase = true
.pattern = "([^a-z])(is)([^a-z])"
Response.Write "<br>VBScript: " & .replace(str,"$1<a
href=is.htm>$2</a>$3")
end with
</script>
<script language="JScript" runat="SERVER">
Response.Write("<br>JScript: " + "Is this what his is_test is? - Is IS
iS - yes, it is".replace(/([^a-z])(is)([^a-z])/gi,"$1<a
href=is.htm>$2</a>$3"));
</script>

For the OP, here are links to regular expression syntax in VBScript and
JScript respectively:
http://msdn.microsoft.com/library/en-us/script56/html/vspropattern.asp
http://msdn.microsoft.com/library/en-us/script56/html/js56jsgrpregexpsyntax.asp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top