ASP VBScript Replace Non Text / Numeric

Discussion in 'C#' started by mrbrantley, Apr 4, 2008.

  1. #1
    I am having a tough time finding a string based replace function to replace anything not A-Z or 0-9 , including spaces, with a dash ( - ). I have found RegEx options, but I am using the code inside of a repeat region, so it will not work. I am trying to make some clean URL's, but cannot get this right. Any help appreicated. Below is one of the many conbinations I have already tried.


    <%=(Replace(rsGetData.Fields.Item("Title").Value, "[a-z0-9]*", "-")) %>
    Code (markup):

     
    mrbrantley, Apr 4, 2008 IP
  2. irock2

    irock2 Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    What you have to do here is pick through each character in the string and determine if it's not acceptable and then replace that character in the string. Something like the following:

    
    Dim aChar As Char
    Dim theTitle As String
    theTitle = rsGetData.Fields.Item("Title").Value
    For Each aChar In theTitle
       If Not ((aChar >= "0" And aChar <= "9") Or (aChar >= "A" And aChar <= "Z") Or (aChar >= "a" And aChar <= "z")) Then
          Replace(theTitle, theChar, "-")
       End If
    Next
    
    Code (markup):
     
    irock2, Apr 5, 2008 IP
  3. InfoSmith

    InfoSmith Peon

    Messages:
    884
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #3
    
    Function kCheckRegExp(vPattern, vStr)
    	Dim oRegExp
    	Set oRegExp = New RegExp
    	oRegExp.Pattern = vPattern
    	oRegExp.IgnoreCase = False
    	kCheckRegExp = oRegExp.Test(vStr)
    	Set oRegExp = Nothing
    End Function
    
    Function kLeachRegExp(vStr1, vPattern, vStr2)
    	Dim oRegExp
    	Set oRegExp = New RegExp
    	oRegExp.Pattern = vPattern
    	oRegExp.IgnoreCase = True
    	oRegExp.Global = True
    	kLeachRegExp = oRegExp.Replace(vStr1, vStr2)
    	Set oRegExp = Nothing
    End Function
    
    Code (markup):
    vstr = kLeachRegExp("string with not a-z 0-9", "[^a-z0-9]+", "")
     
    InfoSmith, Apr 6, 2008 IP