首页 > 解决方案 > 经典 ASP 中的多个 RegEx 模式

问题描述

我在经典 ASP 中使用正则表达式进行验证。我想验证两个不同的值,并检查是否只有两个模式中提到的列出的字符和符号用于该值,并且我尝试为此使用两个不同的 Regex 对象。但这对我不起作用

这是我的代码

Set re = New RegExp
With re
  .Pattern = "[a-zA-Z0-9\&_+()/-]"
  .Global = True
  .IgnoreCase = True
End With 

Set reNew  = New RegExp
With reNew
  .Pattern = "[a-zA-Z0-9.!\"£$%^&()_+-=[]{}#:@./<>?\\|]"
  .Global = True
  .IgnoreCase = True
End With 

  if re.Test(strComments) = false   then
                    response.write " <label>Upload failed !! Please enter comments using valid characters a-z A-Z 0-9 \._+()%/&-</label>"
                    response.end  
                    else             
                          if reNew.Test(strremark) = false  then
                          response.write "<label> Upload failed !! Please enter remark using valid characters a-z A-Z 0-9 \._+()%/&-</label>"
                          response.end 
                          end if
  end if

谁能帮我理解我在哪里犯了错误?

标签: regexasp-classic

解决方案


不需要多个RegExp对象实例,唯一改变的是模式,它只是一个字符串,所以将字符串存储在一个变量中并替换每个Test().

Dim stringToTest: stringToTest = "..." 'Your test string
Dim validationPattern1: validationPattern1 = "..."
Dim validationPattern2: validationPattern2 = "..."
Dim re: Set re = New RegExp
'Set regex properties
With re
    .Gloabl = True
    .IgnoreCase = True
End With
'Before testing add the pattern
re.Pattern = validationPattern1
If Not re.Test(stringtoTest) Then
    'Validation1 failure logic here
    '...
End If
'Add the next pattern
re.Pattern = validationPattern2
If Not re.Test(stringtoTest) Then
    'Validation2 failure logic here
    '...
End If

推荐阅读