A simple string substitution won’t work here, you will have to use an anchored regex. This means that you never match parts of a word, only whole words:
processed_body = processed_body.gsub(/\[email protected]#{mention.user.username}\b/,
"<a href='/user/#{mention.user.id}'>@#{mention.user.username}</a>")
Here we replaced the pattern string with a regex and used \b
to anchor it to word boundaries.
CLICK HERE to find out more related problems solutions.