jsonregexstringjq

Escape Regex special characters in jq


I want to do search and replace in a larger jq program. The problem is that search may contain special characters that are interpreted as regex functionality, which I dont want. Special characters should be treated like normal characters.

So what I need is a regex format escaping function, similar to @uri, @html or @sh. How can I escape all special characters that have meaning in regular expressions (brackets, ., ?, *, + etc.) in the search string so that they are not interpreted as regex syntax?

TEXT='Hello $ spaceman Spiff'
SEARCH='Hello $'   # does not work, because '$' is interpreted as regex special character
REPLACE='Ola '
echo "$TEXT" | \
jq -rR --arg search "$SEARCH" --arg replace "$REPLACE" 'gsub($search; $replace; "g")'

In the original program, 'search' and 'replace' are defined in an external file (passed here by parameter) and the replacement takes place as part of a larger processing of text data coming from another external source.


Solution

  • This should do the trick:

    def deregex:
      reduce ("\\\\", "\\*", "\\^", "\\?", "\\+", "\\.", "\\!", "\\{", "\\}", "\\[", "\\]", "\\$", "\\|", "\\(", "\\)" ) as $c 
        (.; gsub( $c; $c));
    

    Example:

    "^.*$",
     "/*",
     "*/"
    | [., deregex]
    

    produces:

    ["^.*$","\\^\\.\\*\\$"]
    ["/*","/\\*"]
    ["*/","\\*/"]