VBScript preg_grep function

A VBScript equivalent of PHP’s preg_grep


Const PREG_GREP_INVERT    = 1
Function preg_grep(pattern, input, flags)

    Dim obj
    set obj = Server.CreateObject("Scripting.Dictionary")

    If not isArray(input) and not isObject(input) Then
        set preg_grep =  obj
        Exit Function
    End If

    Dim key
    If isArray(input) Then
        For key = 0 to uBound(input)
            If flags = PREG_GREP_INVERT Then
                If Not preg_match(pattern, input(key),",",") Then
                    obj.Add key, input(key)
                End If
            Else
                If preg_match(pattern, input(key),",",") Then
                    obj.Add key, input(key)
                End If
            End If
        Next
    ElseIf isObject(input) Then
        For Each key In input
            If flags = PREG_GREP_INVERT Then
                If Not preg_match(pattern, input(key),",",") Then
                    obj.Add key, input(key)
                End If
            Else
                If preg_match(pattern, input(key),",",") Then
                    obj.Add key, input(key)
                End If
            End If
        Next
    End If

    set preg_grep =  obj

End Function

Please also note that php.vbs offers community built functions and goes by the McDonald’s Theory. We’ll put online functions that are far from perfect, in the hopes to spark better contributions. Do you have one? Then please just:

Other PHP functions in the regexp extension