How to replace a special character in moon?

Asked

Viewed 62 times

2

I’m developing a hangman’s game, but I found a problem, I can’t replace the character ã.

Follows the code:

local done = false
local words = {
    "bola", "casa", "avião"
}


local randomWord = words[math.random(#words)]

while not done do
    print(randomWord)
    print(string.gsub(randomWord, "%a", "_"))
    done = true
end

Result: -ã-

1 answer

1

Lua uses the isalpha of C to make expression matching Pattern %a, by definition it will only accept characters between 'to' and 'z' and 'To' and 'Z', soon won’t take the 'ã' as one. In your case a simple . will solve the problem:

print(string.gsub(randomWord, ".", "_"))

The . in regex serves to pick up any character.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.