2

I have a JSON string and am trying to remove all the special characters (i.e. those defined by the regex [!@#$%^’&*(),.?":{}|<>]) from it.

Some of the jq expressions I've tried are as follows:

sub("[!@#$%^’&*(),.?":{}|<>]"; "") 

sub("/[!@#$%^’&*(),.?":{}|<>]/g"; "")

sub("/\W|_/g"; "")

Can anybody help?

peak
  • 105,803
  • 17
  • 152
  • 177
Lukas
  • 7,384
  • 20
  • 72
  • 127

2 Answers2

1

Use gsub(_;"") or sub(_;"";"g") where _ is a jq expression evaluating to the relevant JSON string specifying the regex.

Since _ must be a jq expression, the jq escaping rules for strings apply, e.g. to escape double quotes, use \". String interpolation is also allowed.

So in the present instance, _ could be:

"[!@#$%^’&*(),.?\":{}|<>]"

See also the jq docs and Regex pattern including all special characters

peak
  • 105,803
  • 17
  • 152
  • 177
0

The final working solution:

tags: (.title
      | sub("([!@#$%^’&*(),.?|])"; ""; "g")
      | split(" ")

and, escaping the apostrophes or single quotes (if needed):

tags: (.title
      | sub("([!@#$%^’'"'"';:&*(),.?|])"; ""; "g")
      | split(" ")
Lukas
  • 7,384
  • 20
  • 72
  • 127
  • In jq, to escape double quotes, use `\"`. jq does not require one to escape single quotes. Your shell of course may complicate things, e.g. in bash: `jq -nr $'"\'"'` – peak Nov 17 '19 at 16:45