Sorry for being unclear, I would like to do this the way I've shown(global matching or replacement and in one line). It doesn't matter if the string is modified or not. In my attempt, it is modified because of the way I am trying to do it.
But if you have a similar solution that doesn't modify the string, it's also fine.