lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


> I have to change a whole word (s) with another word (sh) in a string
> (line), using gsub, but I can't find a simple way to do it.
> 
> Suppose the first word (s) is "the"; I want to change every occurrence of
> "the" but not to change the "the" in "theory", "atheist" or "lithe".

function change (src, find, rep)
	src = gsub (src, "(%w+)", function (word)
		if word == %find then
			return %rep
		else
			return word
		end
	end)
	return src
end

> print(change("the theory abcthedef", "the", "THE"))
THE theory abcthedef

	Is it what you wanted?

		Tomas