paolo2504 wrote: ↑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 said you that on Feb/28:
Aacini wrote: ↑28 Feb 2022 16:33
An additional problem in your case is that the backslash must be preceded by an additional backslash always, and the point must be preceded by a backslash when it is used in a regex.
. . .
Antonio
Perhaps I wasn't clear enough?
Consider these lines from the same post:
Code: Select all
// First pass: replace substrings in *all* file contents
var repl = new Array();
repl[".\\"] = "";
repl["\\\\"] = "";
repl["\\image"] = "image";
Contents = Contents.replace(/\.\\|\\\\|\\image/g, function (A) {return repl[A]});
The first string you were looking for was
".\" "point-backslash". However, because
"the backslash must be preceded by an additional backslash always", in this statement:
... the backslash is written as double-backslash, but in this line:
Code: Select all
Contents = Contents.replace(/\.\\|\\\\|\\image/g, function (A) {return repl[A]});
... the point is also preceded by backslash because
"the point must be preceded by a backslash when it is used in a regex".
I assumed you would understand that these rules apply
just in JScript code and that you knew how to write special characters in a Batch file. If you want to write a percent-sign in a Batch file you must write
%% always, but not in JScript. If you want to write a backslash in JScript you must write
\\ always, but not in Batch.
How do you must write characters in Batch that will be used in JScript? Depends on
the way they will be used in JScript.
If you want to
literally expand a string to be parsed in JScript, you must write it in Batch
in the same way as you would write it in JScript:
Code: Select all
rem In Batch:
set findchr="\.\\|\.\/|\\\\|image=\\"
cscript //nologo //E:JScript "%~F0" %findchr%
// In JScript:
var regexFile = eval("/"+args.Item(0)+"/gi");
If you want to
process the string in JScript as a string, just define the string in Batch in the right way. A string is the same string in Batch or JScript (or any other target):
Code: Select all
rem In Batch:
set findchr=".\|./|\\|image=\"
cscript //nologo //E:JScript "%~F0" %findchr%
// In JScript:
var regexFile = new RegExp(args.Item(0),"gi");
Of course, if you want to search for a percent-sign in JScript, you must write it as double-percent in Batch:
... and if you want to put a backslash in JScript, you must write it as double-backslash even if it is not used as a regexp:
Code: Select all
var myString = "Hello \\world\\"; // the string "Hello \world\"
Antonio
PS - I suggest you to use this form when you assemble your Batch-JScript hybrid scripts.
Standard form:
Code: Select all
@if (@CodeSection == @Batch) @then
@echo off
rem The Batch code section goes here
. . .
goto :EOF
@end
// The JScript code section goes here
. . .
The purpose of the first line is to introduce a method (command/statement) that be valid in both Batch and JScript, and that cause that the Batch code section be ignored by JScript. In Batch: it is a valid IF command that is false (because "
(@CodeSection" string is different than "
@Batch)" string) so the
@then "command" is not executed. In JScript it is an
@if conditional compilation statement that will ignore the following text until the
@end delimiter string if the value between parentheses is zero (false). This means that
@if (0) would give the same result, but when I proposed this method I though that using @CodeSection == @Batch expression and @then "command" (delimiter) would be clearer for people that know nothing about JScript. The
@ signs are required by JScript syntax.
NOTE: There are other methods to do the same thing that, IMHO, does not provide any advantage but the opposite result. For example:
In this case the @X and @Y don't provide any additional information, but this syntax is tricky because the value (@X) is enough for JScript syntax, but not for Batch. In this way, writting
any string after the double-equal sign would be enough to complete the Batch syntax. For example:
@if (@X) == Y. Why write the Y also enclosed in parens and preceded by @-sign? Just to make this trick more esoteric and incomprehensible, perhaps? I really don't know...
A particularly strange form is this one:
In this case the IF Batch statement
is true, so a valid Batch command must be placed ahead, in the same line...
Another simpler way to write a Batch-JScript hybrid script is this:
Simplified form:
Code: Select all
@set @a=0 /*
@echo off
rem The Batch code section goes here
. . .
goto :EOF
*/
// The JScript code section goes here
. . .
In this case the purpose of the first line is to introduce a JScript multi-line comment
/* that will ignore the Batch file section until the complementary
*/ close comment characters. You could put the
/* characters alone in a line, but that would cause an error in Batch code. The simplest way to avoid the Batch error is using the @set command, that in Batch is valid and in JScript is the
@set conditional compilation statement.
NOTE: Some authors also use this method:
In this case, the
@if statement, which can skip a multi-line section by itself, is closed and a multi-line comment is immediately started.
IMHO, this is a "waste" of the capabilities of the @if statement... (Why make things more complicated than necessary?)