Search code examples
batch-filecmdjrepl

JREPL.bat regex replacing inside quotes


Im using JREPL.BAT to find and replace specific instances and my regex I have works for find and replace in VSC code and also in the couple regex editors I've used.

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

so what I'm expecting is it to find any case of

Error("")

or

Error( skjdksjdskd() + "" + )

etc

Find and replace works perfectly but jrepl takes

Error( skjdksjdskd() + "" + )

and changes it to

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

does anyone know with more JREPL experience know why its ignoring the quotes and also not replacing the () area?


Solution

  • JREPL is hybrid JScript/batch that uses CSCRIPT - the Windows script host.

    CSCRIPT has an inherent limitation that prevents double quote literals from being passed as parameters - there is no CSCRIPT escape sequence that includes a " literal.

    To include a " literal in your query string, you can use \x22 instead. All of the standard JScript escape sequences can be used in the query string. By default, escape sequences are not recognized in the replace string.

    But you want a quote literal in your replace string. This requires the /XSEQ option so you can use the JREPL extended escape sequence of \q. A significant advantage of this option is you can also use the extended escape sequences in the replace string. You could also use \x22 for both the search and replace strings if you prefer, but I find \q much easier to remember.

    You have one other potential problem - the CALL command doubles all quoted carets, so [^()] (any character other than ( or )) becomes [^^()] (any character other than ^, ( or )). This is definitely not what you want. That is the reason I added the \c = ^ extended escape sequence.

    So I believe the following will give your expected result:

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

    FYI - The effect of the ^ beginning of string anchor is not harmed by caret doubling - you don't need the \c escape sequence for the beginning of string anchor because "^MatchStringBeginning" and "^^MatchStringBeginning" yield identical regex results.

    You can get more information about the extended escape sequences by issuing jrepl /?/xseq, or jrepl /??/xseq for paged help.

    >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.
    

    Final Answer for this is to escape the quotes and backslashes as \" AND \\ when using CALL in Webpack-shell-plugin.

    'call "./framework/config/JREPL.BAT" \"(Error)\\(([\\c()]*|\\(([\\c()]*|\\([\\c()]*\\))*\\))*\\)\" \"Error(\\q\\q)\" /xseq /f ./dist/index.html /o ./dist/indexFinal.html'