469,286 Members | 2,466 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,286 developers. It's quick & easy.

Replace in ASP/VBS/SQL

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
Jul 19 '05 #1
8 6066
replace " is " instead of "is"
--
----------------------------------------------------------
Curt Christianson (Software_AT_Darkfalz.Com)
Owner/Lead Designer, DF-Software
http://www.Darkfalz.com
---------------------------------------------------------
...Offering free scripts & code snippits for everyone...
---------------------------------------------------------

"Pret Orian" <pr**********@yahoo.com> wrote in message
news:98**************************@posting.google.c om...
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

Jul 19 '05 #2
Pret Orian wrote:
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/scr...er/default.asp

Microsoft® Windows®2000 Scripting Guide
http://www.microsoft.com/technet/scr...s_overview.asp

Jul 19 '05 #3
pr**********@yahoo.com (Pret Orian) wrote in message news:<98**************************@posting.google. com>...
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.
Jul 19 '05 #4

"Pret Orian" <pr**********@yahoo.com> wrote in message
news:98**************************@posting.google.c om...
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
Jul 19 '05 #5
>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
Jul 19 '05 #6
Hi,

"Pret Orian" <pr**********@yahoo.com> wrote in message
news:98**************************@posting.google.c om...
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

Jul 19 '05 #7
Pret Orian wrote:
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


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/scr...er/default.asp

Microsoft® Windows®2000 Scripting Guide
http://www.microsoft.com/technet/scr...s_overview.asp

Jul 19 '05 #8
"Michael Harris (MVP)" <mi****@mvps.org> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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/scr...er/default.asp

Microsoft® Windows®2000 Scripting Guide
http://www.microsoft.com/technet/scr...s_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...propattern.asp
http://msdn.microsoft.com/library/en...gexpsyntax.asp
Jul 19 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Craig Keightley | last post: by
6 posts views Thread by JackpipE | last post: by
15 posts views Thread by =?Utf-8?B?TWlrZSAiWU9fQkVFIiBC?= | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.