[jq] Replace every regular expression match with gsub

Created on November 30, 2026

Use gsub to replace all the parts of a string that match a regular expression. The first argument is the pattern and the second is the replacement text. Separate them with a semicolon.

printf '%s\n' '"apple pie, apple juice"' | jq 'gsub("apple"; "orange")'
"orange pie, orange juice"

Use sub to replace just the first match.

printf '%s\n' '"apple pie, apple juice"' | jq 'sub("apple"; "orange")'
"orange pie, apple juice"

Reference

jq