Home > database >  Escape Regex special characters in jq
Escape Regex special characters in jq

Time:12-26

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.

CodePudding user response:

This should do the trick:

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

Example:

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

produces:

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

CodePudding user response:

The limits of working without functions that use regexes may be peferable to the risks of missing any regex syntax, for example with split/1:

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" 'split($search)|join($replace)'
  • Related