Hello! I’m trying to replace a word within a string with another word. Whilst string.gsub() works for most cases, it may cause troubles for short words, like “me”. For example:
local str = "He is meeting me"
local result = string.gsub(str,"me","her")
print (result) -- "He is hereting her", obviously wrong
I’ve tried playing around string patterns, however, I encountered some problems. Whilst using "%Ame%A" works for most cases, it doesn’t work whenever the word in at the end or at the beginning of a sentence. For example:
local str = "me me me"
local result = string.gsub(str,"%Ame%A"," her ")
print (result) -- "me her me", also wrong
Is there a way to make it so it also matches the "me"s at the beginning and at the end?
Furthermore, is there a way to preserve the punctuation around the word? For example, using the code above, "me,me,me" would turn to "me her me".