Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regex replace characters in string 1

Status
Not open for further replies.

madanthrax

IS-IT--Management
Sep 15, 2001
123
AT
Hi,

Some time ago I stored away some code posted by Tarwn in a reply, thinking I might need it one day.

Code:
dim teststring
teststring = "this is a test: slash\ bracket{ bracket} singlequote' lparen( rparen) whee"

Response.Write "teststring Unchanged is: " & teststring & "<br/>"

Response.Write "teststring Changed is: " & DeleteChars(teststring)

Function DeleteChars(str)
    Dim regex
    Set regex = New RegExp
    regex.Pattern = "([\\{}'()])"
    regex.Global = True
    DeleteChars = regex.Replace(str,"")
    Set regex = Nothing
End Function
This works fine for deleting individual characters from form input strings (although I have no idea why there are two slashes in the Pattern).

I am a Regex newbie but after an js/iframe/injection attack on my website I have been experimenting with the pattern unsucessfully to delete multiple character substrings such as occurences of http:// and .js.

My objective is to filter out several individual characters and these two substrings from user form text inputs. The script above seems so simple but the Regex help websites are so complicated in respect to patterns.

Am I starting from the wrong end or is there a way to include substrings in the regex pattern?

Thanks for any help,

Anthony

[sub]&quot;Nothing is impossible until proven otherwise&quot;[/sub]​
 
[tt]dim s,rx,t
s="Appearance of protocol such as [ignore][/ignore], and anything like .JS, such as xyz.js, will be sanitized."

set rx=new regexp
with rx
.global=true
.ignorecase=true
.pattern="([ignore]http://[/ignore]|.js)"
end with
t=rx.replace(s,"[$1]") 'Replace 2nd argument by "" if you want to.
response.write s & "<br />" & t
[/tt]
 
Amendment
I should have put this instead to be exact.
[tt] .pattern="([red]\.[/red]js)"[/tt]
 
tsuji,

Thanks for the reply and pattern.
If I wanted to add your pattern to the original pattern "([\\{}'()])" , is this possible? I am not sure what the square brackets are there for...

[sub]&quot;Nothing is impossible until proven otherwise&quot;[/sub]​
 
>If I wanted to add your pattern to the original pattern "([\\{}'()])" , is this possible?
Sure, to follow.

>I am not sure what the square brackets are there for...
If you're referring to my just-for-illustration replacement by [tt]"[$1}"[/tt], it has no special meaning. It is only preserving the trace of the original message line so that you can (a) see or (b) for other to recover the original through a fixed algorithm. That is a non-issue. Square bracket is just a means to highlighting. You are free any other similar means to acheive the same purpose.

The pattern would simply look like this.
[tt] .pattern="([\\{}'()])"[/tt]
 
Fantastic, thanks for your help.

[sub]&quot;Nothing is impossible until proven otherwise&quot;[/sub]​
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top