How can I sustitute a certain character using string.gsub?

Hello, as the title says I am trying to remove a certain character from a string using the mentioned function from the string library. The only allowed characters should be uppercase and lowercase letters and an underscore. The code I wrote is the following which removes characters who do not belong to the class of uppercase and lowercase characters.

local function removeUnwantedCharacters(string)
return string.gsub(string, "%A", "")
end

What I’m wondering is how could I make it an exception with the underscore?

If I’m reading your post correctly, this is what you wanted right?

local function removeUnwantedCharacters(string)
    return string.gsub(string, "[^%a_]", "")
end

^ negates the set, meaning it matches any character that is not in the set.
%a is a shortcut for all uppercase and lowercase letters.
_ includes the underscore character in the set.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.