How to find and replace parts or text

I am working on a text editor, and I am using a textbox. I want to be able to find text that the use writes and replace it with a different word. For example: If a user writes “hello” I want to replace the hello with hello, I tried using string.split and I get every single word. I then use a for loop to loop through the words and see if any of them is hello and then replace. But I don’t know how I would change the text on the textbox itself with this. Here is my script so far:

--To Color use <font color="">TEXT</font>
function SetTextColor(Spliced)
	local Text = ""
	for i = 1,#Spliced do
		Text = Text.." "..Spliced[i]
	end
	script.Parent.Text = Text
end

local RedText = {
	"if"
}

while wait() do
	local TextSpliced = string.split(script.Parent.Text," ")
	for i = 1,#TextSpliced do
		if RedText[TextSpliced[i]] then
			TextSpliced[i] = '<font color="#ff005d">'..TextSpliced[i].."</font>"
		end
	end
	SetTextColor(TextSpliced)
end
1 Like

Replacing text:

local text = "Hello world!"
text = text:gsub("Hello", "Goodbye") 
print(text) --Goodbye world!
1 Like

As @NyrionDev said above, you can use .gsub. You can find a more indepth view Here
gsub stands for “global substitution” and is used in this format (said by the link): (string s, string pattern, variant replacement, number replacements)

If I use gsub, then will the text be changed for all of the words?
E.g. “Hello world! Hello world! Hello world!”

Will it make all the hellos into goodbyes?

1 Like

By default yes, although gsub has a third parameter called replacements which defines the amount of words it replace until it stops.

--without replacements parameter
local text = "Hello world! Hello world!"
text = text:gsub("Hello", "Goodbye") 
print(text) --Goodbye world! Goodbye world!

--with replacements parameter
local text = "Hello world! Hello world!"
text = text:gsub("Hello", "Goodbye", 1) 
print(text) --Goodbye world! Hello world!

Since I replace the text every task.wait(), the words just turn into this every time I type “if”:


is there a way to fix this?

This may happen due to magic characters, which you can filter before passing to the gsub:

function filterMagicCharacters(text)
	local magic = {"$", "%","^", "*", "(", ")", ".", "[", "]", "+", "-", "?"}
	local result = ""
	for i = 1, string.len(text) do 
		local l = string.sub(text, i, i)
		if table.find(magic, l) then 
			l = "%"..l 
		end
		result ..= l 
	end
	return result 
end

print(filterMagicCharacters("[]test")) --%[%]test
--pass this to the gsub
1 Like

It still does the thing though. It assumes that the “if” inside the <font color="colorValue">if</font> is the if it is looking for. For testing, I replaced “if” with amogus. Here is a video:

Every time you update the text use script.Parent.ContentText as the starting value(instead of script.Parent.Text which also contains the richtext tags).

Nope, that didn’t work. Before using gsub, is there a way I can see if the text is already replaced? Like seeing if there are richtext tags around it? Also, is there a way to only show the reformatted text instead of showing the richtext tags when writing? So that nobody breaks the script?