JREPL.bat 正则表达式替换内引号

JREPL.bat regex replacing inside quotes

我正在使用 JREPL.BAT 来查找和替换特定实例,而我的正则表达式可以在 VSC 代码以及我使用的几个正则表达式编辑器中进行查找和替换。

CALL ./framework/config/JREPL.BAT "(Error)+\(([^()]*|\(([^()]*|\([^()]*\))*\))*\)" "Error(\"\")" /f ./dist/index.html /o

所以我期待的是找到

的任何情况
Error("")

Error( skjdksjdskd() + "" + )

等等

查找和替换工作完美,但 jrepl 需要

Error( skjdksjdskd() + "" + )

并将其更改为

Error()( skjdksjdskd() + "" + )

有没有人知道有更多 JREPL 经验的人知道为什么它忽略引号也不替换 () 区域?

JREPL 是混合体 JScript/batch,它使用 CSCRIPT - Windows 脚本宿主。

CSCRIPT 有一个固有的限制,阻止双引号文字作为参数传递 - 没有包含 " 文字的 CSCRIPT 转义序列。

要在查询字符串中包含 " 文字,您可以改用 \x22。所有标准的 JScript 转义序列都可以在查询字符串中使用。默认情况下,替换字符串中不识别转义序列。

但是您想要在替换字符串中使用引号文字。这需要 /XSEQ 选项,以便您可以使用 \q 的 JREPL 扩展转义序列。此选项的一个显着优点是您还可以在替换字符串中使用扩展的转义序列。如果愿意,您也可以将 \x22 用于搜索和替换字符串,但我发现 \q 更容易记住。

您还有一个潜在的问题 - CALL 命令将所有引用的插入符号加倍,因此 [^()]() 以外的任何字符)变为 [^^()]^() 以外的任何字符)。这绝对不是你想要的。这就是我添加 \c = ^ 扩展转义序列的原因。

所以我相信以下会给出您预期的结果:

CALL .\framework\config\JREPL.BAT "(Error)+\(([\c()]*|\(([\c()]*|\([\c()]*\))*\))*\)" "Error(\q\q)" /xseq /f .\dist\index.html /o -

仅供参考 - ^ 字符串锚点开头的效果不受插入符加倍的影响 - 你不需要 \c 字符串锚点开头的转义序列,因为 "^MatchStringBeginning""^^MatchStringBeginning" 产生相同的正则表达式结果。

您可以通过发出 jrepl /?/xseqjrepl /??/xseq 获取分页帮助来获得有关扩展转义序列的更多信息。

>jrepl /?/xseq

      /XSEQ - Enables extended escape sequences for both Search strings and
            Replacement strings, with support for the following sequences:

            \     -  Backslash
            \b     -  Backspace
            \c     -  Caret (^)
            \f     -  Formfeed
            \n     -  Newline
            \q     -  Quote (")
            \r     -  Carriage Return
            \t     -  Horizontal Tab
            \v     -  Vertical Tab
            \xnn   -  Extended ASCII byte code expressed as 2 hex digits nn.
                      The code is mapped to the correct Unicode code point,
                      depending on the chosen character set. If used within
                      a Find string, then the input character set is used. If
                      within a Replacement string, then the output character
                      set is used. If the selected character set is invalid or
                      not a single byte character set, then \xnn is treated as
                      a Unicode code point. Note that extended ASCII character
                      class ranges like [\xnn-\xnn] should not be used because
                      the intended range likely does not map to a contiguous
                      set of Unicode code points - use [\x{nn-mm}] instead.
            \x{nn-mm} - A range of extended ASCII byte codes for use within
                      a regular expression character class expression. The
                      The min value nn and max value mm are expressed as hex
                      digits. The range is automatically expanded into the
                      full set of mapped Unicode code points. The character
                      set mapping rules are the same as for \xnn.
            \x{nn,CharSet} - Same as \xnn, except explicitly uses CharSet
                      character set mapping.
            \x{nn-mm,CharSet} - Same as \x{nn-mm}, except explicitly uses
                      CharSet character set mapping.
            \unnnn -  Unicode code point expressed as 4 hex digits nnnn.
            \u{N}  -  Any Unicode code point where N is 1 to 6 hex digits

            JREPL automatically creates an XBYTES.DAT file containing all 256
            possible byte codes. The XBYTES.DAT file is preferentially created
            in "%ALLUSERSPROFILE%\JREPL\" if at all possible. Otherwise the
            file is created in "%TEMP%\JREPL\" instead. JREPL uses the file
            to establish the correct \xnn byte code mapping for each character
            set. Once created, successive runs reuse the same XBYTES.DAT file.
            If the file gets corrupted, then use the /XBYTES option to force
            creation of a new XBYTES.DAT file. If JREPL cannot create the file
            for any reason, then JREPL silently defaults to using pre v7.4
            behavior where /XSEQ \xnn is interpreted as Windows-1252. Creation
            of XBYTES.DAT requires either CERTUTIL.EXE or ADO. It is possible
            that both may be missing from an XP machine.

            Without the /XSEQ option, only standard JSCRIPT escape sequences
            \, \b, \f, \n, \r, \t, \v, \xnn, \unnnn are available for the
            search strings. And the \xnn sequence represents a unicode
            code point, not extended ASCII.

            Extended escape sequences are supported even when the /L option
            is used. Both Search and Replace support all of the extended
            escape sequences if both the /XSEQ and /L options are combined.

            Extended escape sequences are not applied to JScript code when
            using any of the /Jxxx options. Use the decode() function if
            extended escape sequences are needed within the code.

最终答案是在 Webpack-shell-plugin.

中使用 CALL 时将引号和反斜杠转义为 \" AND \
'call "./framework/config/JREPL.BAT" \"(Error)\(([\c()]*|\(([\c()]*|\([\c()]*\))*\))*\)\" \"Error(\q\q)\" /xseq /f ./dist/index.html /o ./dist/indexFinal.html'