SoFunction
Updated on 2025-04-07

Use of regular expressions ASP


<%
' --------------------------------------------------------------
' Match object

' The result of the matching search is stored in the Match object, providing access to read-only properties that are matched by regular expressions.
' Match object can only be created through the Execute method of the RegExp object, which actually returns a collection of the Match object.
' All Match object properties are read-only. When executing regular expressions, zero or more Match objects may be generated.
' Each Match object provides access to the string found by regular expression search, the length of the string, and the matching index position found.
' ○   FirstIndex property, returns the matching position in the search string. The FirstIndex property uses an offset calculated from zero, which is relative to the starting position of the search string. In other words, the first character in the string is identified as a character 0
' ○    Length property, returns the length of the match found in the string search.
' ○   Value     Value   property, returning the matching value or text found in a search string.
' --------------------------------------------------------------
'  RegExpExecute("[ij]s.", "IS1 Js2 IS3 is4")
Function RegExpExecute(patrn, strng)
Dim regEx, Match, Matches             'Create variables.
SET  regEx= New RegExp                                                                                                SET     regEx = New RegExp         �
= patrn                 'Set mode.
= True              'Set whether it is case-insensitive.
= True                            'Set global availability.
SET  Matches = (strng)   'Perform a search.
For Each Match in Matches                'Travel over the matching collection.
        RetStr = RetStr & "Match found at position "
        RetStr = RetStr &  & ". Match Value is '"
        RetStr = RetStr &  & "'." & "<BR>"
    Next
    RegExpExecute = RetStr
End Function

' --------------------------------------------------------------------
' Replace Method
' Replace the text found in regular expression search.
' --------------------------------------------------------------------
'  RegExpReplace("fox","cat") & "<BR>"                             Replace 'fox' with 'cat'.
'  RegExpReplace("(S+)(s+)(S+)", "$3$2$1")    ' Exchange word pair.
Function RegExpReplace(patrn, replStr)
Dim regEx, str1
    str1 = "The quick brown fox jumped over the lazy dog."
SET  regEx= New RegExp                                                                 SET                    SET     regEx = New RegExp                   �
= patrn           'Set mode.
= True         'Set whether it is case-insensitive.
RegExpReplace = (str1, replStr)    ' For replacement.
End Function

' --------------------------------------------------------------------
' Use the Test method to search.
' Perform a regular expression search on the specified string and return a Boolean value
' Indicates whether a matching pattern is found. The actual pattern of regular expression search is set through the Pattern property of the RegExp object.
'  The attribute has no effect on the Test method.
' If a matching pattern is found, the Test method returns True; otherwise, it returns False
' --------------------------------------------------------------------
'  RegExpTest("Function", "Important Function")
Function RegExpTest(patrn, strng)
Dim regEx, retVal
SET  regEx= New RegExp                                                                 SET                    SET     regEx = New RegExp                   �
= patrn           'Set mode.
= False      'Set whether it is case-insensitive.
retVal= (strng)        ' Perform the search test.
    If retVal Then
RegExpTest = "One or more matches found."
    Else
RegExpTest = "No match found."
    End If
End Function
%>