Quantcast
Channel: Active questions tagged jq - Stack Overflow
Viewing all articles
Browse latest Browse all 657

Escape Regex special characters in jq

$
0
0

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 characterREPLACE='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.


Viewing all articles
Browse latest Browse all 657

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>