This would be clearer with a helper function like:

```R
replace_special <- function(x, name, value) {
  gsub(paste0("([^%]|^), name), paste0("\\1", value), x)
}
```