Look, the pattern in gsub("[\\]]","==","hello\\] [world]")
, [\]]
, is effectively matching a \
followed with ]
. Try gsub("[\\]]","==","hello\\] [world]")
, and the result will be hello== [world]
, the literal backslash will get replaced.
In a TRE regex pattern, a \
inside a bracket expression matches a literal backslash.
As a fix for your "[\\]]"
regex, you may remove the \
from the pattern:
gsub("[[]","==","hello [world]")
See this R online demo.
You may escape it in a PCRE pattern though, as PCRE character classes allow escaped characters inside them:
gsub("[\\[]","==","hello [world]", perl=TRUE)
See another demo
If you need to replace either [
or ]
, just put the ][
inside the bracket expression:
gsub("[][]","==","hello [world]")