Variable vs arguments containing special chars
Posted: 12 Mar 2022 12:30
Hi all. I'm just beginner with ms/jscript (jscript under Windows).
I recently faced an issue that i can't understand: i found that variable containing special characters are treated in a different way when passed as arguments from a batch file or set directly inside the jscript code.
I wrote a brief testcode below to show my concern: I would like to understand :
a) why arguments and var directly set behave differently (and how set vars inside the jscript code to fully maintain all special chars)
b) why the eval instruction returns /ig instead of /gi
Appreciate any hint. Thanks in advance.
PS NOTE: I've just found right now that doubling each backslash in var FindstrDirectlySet everything goes fine. I wonder if the argument passed from the batch code
is considered by jscript as an object (not a string) so not to be changed/interpreted....
result
I recently faced an issue that i can't understand: i found that variable containing special characters are treated in a different way when passed as arguments from a batch file or set directly inside the jscript code.
I wrote a brief testcode below to show my concern: I would like to understand :
a) why arguments and var directly set behave differently (and how set vars inside the jscript code to fully maintain all special chars)
b) why the eval instruction returns /ig instead of /gi
Appreciate any hint. Thanks in advance.
PS NOTE: I've just found right now that doubling each backslash in var FindstrDirectlySet everything goes fine. I wonder if the argument passed from the batch code
is considered by jscript as an object (not a string) so not to be changed/interpreted....
Code: Select all
@if (@X)==(@Y) @end /*
@echo off
set findchr="\.\\|\.\/|\\\\|=\\|=\/|image=\\"
cscript //nologo //E:JScript "%~F0" %findchr% 2>d:\jslog.txt
goto :EOF
************* JScript portion **********/
var args = WScript.Arguments;
var FindstrFromArgs=args.Item(0)
var FindstrDirectlySet = "\.\\|\.\/|\\\\|=\\|=\/|image=\\";
var regexFile = eval("/" + FindstrFromArgs + "/gi");
WScript.Stderr.WriteLine("FindstrFromArgs = " + FindstrFromArgs);
WScript.Stderr.WriteLine("FindstrDirectlySet = " + FindstrDirectlySet);
WScript.Stderr.WriteLine("regexFile = " + regexFile);
Code: Select all
FindstrFromArgs = \.\\|\.\/|\\\\|=\\|=\/|image=\\
FindstrDirectlySet = .\|./|\\|=\|=/|image=\
regexFile = /\.\\|\.\/|\\\\|=\\|=\/|image=\\/ig