Automatic % to magic characters

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

for context: im trying to make a plugin thats a better ctrl + f, aka it replacing things within the scripts, ive already got everything down besides including magic characters automatically.
Basically, im trying to add a % before every magic character

  1. What is the issue? Include screenshots / videos if possible!

Ive tried multiple methods, but im not very experienced with strings/tables
for instance, ive tried adding all characters to a table and adding “%” before it, it just crashed studio, ive also tried changing the value in the table to include “%” then the letter, for instance

v = ("%"..v)

but it dosent even change the value

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    the things ive tried are listed above. (sorry for the bad listing)

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

			local text = v.Source
			
			local magicCharacters = {
				".", "^", "$", "(", ")", "[", "]", "*", "+", "-", "?", "!"
			}
			
			
			local characters = string.split(text,"")
			
			for i,v in characters do
				if table.find(magicCharacters,v) then
					v = ("%"..v)
				end
			end

this is one of the things i’ve tried, im mostly just looking for a way to be able to do what im trying to achieve

Thank you for reading!

and i apologize for anything i may have typed or said wrong, please ask questions if you have any confusion

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

if you are trying to replace the character inplace in the table you need to assign to characters[i] instead of v

			local text = v.Source
			
			local magicCharacters = {
				".", "^", "$", "(", ")", "[", "]", "*", "+", "-", "?", "!"
			}
			
			
			local characters = string.split(text,"")
			
			for i,v in characters do
				if table.find(magicCharacters,v) then
					characters[i] = ("%"..v)
				end
			end

Thats definitely strange, but thank you for helping!

also if you want the plugin to be more efficient when dealing with large amounts of text, you can make the magicCharacters table into a set, so that the character can be looked up in O(1) time complexity instead of O(n).

local text = v.Source

local magicCharacters = {
	".", "^", "$", "(", ")", "[", "]", "*", "+", "-", "?", "!"
}

local magicSet = {}
for _, char in ipairs(magicCharacters) do
	magicSet[char] = true
end

local characters = string.split(text,"")

for i,v in characters do
	if magicSet[v] then
		characters[i] = ("%"..v)
	end
end

I dont really understand sets, but whatever sets did actually ended up fixing an error! thank you fo helping!