Function to Escape (i.e., backslash) Regular Expression characters:   Return to List

' So that they are considered as literals (i.e., no special power)

Public Function EscapeOffRegexChars(ByVal strString As String) As String
' Declare our Regex Array
Dim FindTerm(12)
FindTerm(0) = "*"
FindTerm(1) = "@"
FindTerm(2) = "\"
FindTerm(3) = "("
FindTerm(4) = ")"
FindTerm(5) = "["
FindTerm(6) = "]"
FindTerm(7) = "?"
FindTerm(8) = "<"
FindTerm(9) = ">"
FindTerm(10) = "!"
FindTerm(11) = "{"
FindTerm(12) = "}"
For i = 0 To 12
    ' To replace all occurrences of one string in another
    Dim intPos As Integer, intLP As Integer, intLen As Integer, strTemp As String
    ' find each search string and replace with target
    intLen = Len(FindTerm(i))
    intPos = InStr(strString, FindTerm(i))
    While intPos <> 0
        strTemp = strTemp & Left$(strString, intPos - 1)
        strTemp = strTemp & "\" & FindTerm(i)
        strString = Right$(strString, Len(strString) - intPos - intLen + 1)
        intPos = InStr(strString, FindTerm(i))
    Wend
Next
' append remainder upon failure of last InStr search
strTemp = strTemp & strString
EscapeOffRegexChars = strTemp
End Function



Note to Webmaster