我几乎可以肯定我已经知道了这个答案,但我的VBScript非常生疏,所以我希望被证明是错的。
我正在使用一个定制系统,它使用VBScript来评估表单字段的验证。问题是VBScript只能存在于一行上,并且必须返回一个布尔值。
这适用于检查字符串长度以及字符串中是否存在特定字符串,但我需要执行一些正则表达式检查。
所以我的问题是,有没有办法将以下内容压缩到一行?
Set myRegExp = New RegExp
myRegExp.IgnoreCase = True
myRegExp.Global = True
myRegExp.Pattern = "^[\w\s]*$"
myRegExp.Test("Test St*ring")
如果没有IgnoreCase
和Global
属性,该示例可能会存在。
提前致谢,
富
答案 0 :(得分:2)
取决于“定制系统”的工作原理:
#3的POC - 所有你需要的是爱 - 不,我的意思是能够放一个.wsc:
<?xml version="1.0"?>
<component>
<registration
description="wsc trial"
progid="Trial.WSC"
version="1.00"
classid="{F17637AC-48F0-4C02-9A88-D06741FFA58F}"
>
</registration>
<public>
<method name="vld">
<PARAMETER name="sTxt"/>
</method>
</public><script language="VBScript">
<![CDATA[
Function vld( sTxt )
Dim reVld : Set reVld = New RegExp
reVld.IgnoreCase = True
reVld.Global = True
reVld.Pattern = "^[\w\s]*$"
vld = reVld.Test( sTxt )
End Function
]]>
</script></component>
在计算机和线上
GetObject("script:<full path to\trial.wsc").vld( "Test St*ring" )
在那一行。测试脚本:
' Assuming the 'bespoke system' evals the string you put on the line
Dim aTests : aTests = Array( _
"Set reVld = New RegExp : reVld.IgnoreCase = True : reVld.Global = True : reVld.Pattern = ""^[\w\s]*$"" : reVld.Test(""Test St*ring"")" _
, "vld( ""Test St*ring"" )" _
, "GetObject(""script:E:\trials\SoTrials\answers\6907380\vbs\trial.wsc"").vld( ""Test St*ring"" )" _
, "GetObject(""script:E:\trials\SoTrials\answers\6907380\vbs\trial.wsc"").vld( ""nice data"" )" _
)
WScript.Echo String( 79, "-" )
Dim sToEval
For Each sToEval In aTests
Dim vVld : vVld = "Bingo"
On Error Resume Next
vVld = Eval( sToEval )
If 0 <> Err.Number Then vVld = Err.Description
On Error GoTo 0
WScript.Echo Join( Array( _
"to eval: " & sToEval _
, "result: " & CStr( vVld ) _
, String( 79, "-" ) _
), vbCrLf )
Next
Function vld( sTxt )
Dim reVld : Set reVld = New RegExp
reVld.IgnoreCase = True
reVld.Global = True
reVld.Pattern = "^[\w\s]*$"
vld = reVld.Test( sTxt )
End Function
输出:
cscript 6907380.vbs
-------------------------------------------------------------------------------
to eval: Set reVld = New RegExp : reVld.IgnoreCase = True : reVld.Global = True : reVld.Pattern = "^[\w\s]*$"
: reVld.Test("Test St*ring")
result: Syntax error
-------------------------------------------------------------------------------
to eval: vld( "Test St*ring" )
result: False
-------------------------------------------------------------------------------
to eval: GetObject("script:E:\trials\SoTrials\answers\6907380\vbs\trial.wsc").vld( "Test St*ring" )
result: False
-------------------------------------------------------------------------------
to eval: GetObject("script:E:\trials\SoTrials\answers\6907380\vbs\trial.wsc").vld( "nice data" )
result: True
-------------------------------------------------------------------------------
答案 1 :(得分:1)
你有没有尝试过:
Set myRegExp = New RegExp : myRegExp.IgnoreCase = True : myRegExp.Global = True : myRegExp.Pattern = "^[\w\s]*$" : myRegExp.Test("Test St*ring")
来源: http://technet.microsoft.com/en-us/library/ee176989.aspx