Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

String.gsub doesn't work correctly ?

Hi. I recently tried out gsub and figuring how it works, and I ran into a problem. I have a script like this

local charmap = {
	["k"] = 'x',
	["kh"] = 'x',
	["c"] = 'k',
	["c?"] = 'k',
	["c!"] = 'k',
	["ch"] = 'k',
	["cq"] = 'k',
	["t"] = 'c',
	["tr"] = 'c',
	["c"] = 'c',
	["ch"] = 'c',
	["d"] = 'z',
	["g"] = 'z',
	["gi"] = 'z',
	["r"] = 'z',
	["g"] = 'z',
	["gi"] = 'z',
	["gì"] = 'z',
	["gí"] = 'z',
	["g?"] = 'z',
	["g?"] = 'z',
	["g?"] = 'z',
	["?"] = 'd',
	["p"]= 'f',
	["ph"]= 'f',
	["n"] = 'q',
	["ng"] = 'q',
	["nh"] = 'q',
	["n?"] = 'q',
	["t"] = 'w',
	["th"] = 'w'
}

print(string.gsub("tieng", ".", charmap))

but instead of printing "tieq", it prints "tieqw" (look at the character map for reference). Please fix it for me. Thanks in advance.

1 answer

When I tested, it printed what it was supposed to: wieqz.

  • The first letter, t was matched and replaced with w.

  • The third and fourth letters, n and g were matched and replaced with q.

  • The third letter, n, was matched again and "replaced" with z

This is the product of gsub matching two different overlapping patterns.

1 comment

0

is there a workaround for this ?

This discussion is preserved as it was. New answers, comments, and votes are closed.