I'm almost certain I already know the answer to this one, but my VBScript is very rusty so I'm hoping to be proved wrong.
I am using a bespoke system that uses VBScript to evaluate validation on form fields. The problem is that the VBScript can only exist on one line and must return a boolean.
This is fine for checking string lengths and the existence of a specific string within a string, but I need to perform some regular expression checks.
So my question is, is there any way to condense the following to one line?
Set myRegExp = New RegExp
myRegExp.IgnoreCase = True
myRegExp.Global = True
myRegExp.Pattern = "^[\w\s]*$"
myRegExp.Test("Test St*ring")
The example would probably survive without the IgnoreCase
and Global
properties.
开发者_Go百科Thanks in advance,
Rich
Depending on how this 'bespoke system' works:
- Use : to combine many statements on the line
- Add a function to the system's code base and call it on the line
- Add a COM component (.wsc) to the system's environment and call its .Validate method on the line
POC for #3 - all you need is love - no I mean the ability to put a .wsc like:
<?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>
on the computer and the line
GetObject("script:<full path to\trial.wsc").vld( "Test St*ring" )
in that one line. Test script:
' 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
output:
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
-------------------------------------------------------------------------------
Did you try:
Set myRegExp = New RegExp : myRegExp.IgnoreCase = True : myRegExp.Global = True : myRegExp.Pattern = "^[\w\s]*$" : myRegExp.Test("Test St*ring")
Source: http://technet.microsoft.com/en-us/library/ee176989.aspx
精彩评论