How to find and replace words containing particular characters in Lua?
snippet in lua

How to find and replace words containing particular characters in Lua?

user2645

function replace(str)
  return (str:gsub("%S+", function(word)
    if word:match("[ABC]") then return 1 end
    if word:match("[XYZ]") then return 2 end
    return 3
  end))
end

print(replace("the CAT ATE the Xylophone")) --> 3 1 1 3 2