Rich text colors character of escape characters: e.g "&"

Hi there, I have a custom code editor I’ve made with syntax highlighting, but the issue is, is that I’ve tried to use escape characters to prevent unintended symbols from breaking the syntax highlighting in my script. I’ve tried using string.gsub to replace the symbols with the escape characters, but the issue is, is that it still doesn’t prevent some symbols such as & from being highlighted. I don’t know how to fix this thus why I’m asking for help here.

     text = string.gsub(text,"&","&") -- text is the "code" that is typed into the code editor
		text = string.gsub(text,"<","&lt;")
		text = string.gsub(text,">","&gt;")
		text = string.gsub(text,'"',"&quot;")
		text = string.gsub(text,"'","&apos;")
		lines = string.split(highlight(text),"\n")