String.split but keep delimiters?

So I’m trying to create a little script editor but it doesn’t function like I want it to, I want it to keep the delemitters. Like this:

local string_ = "local Instance.new"

I made it so that the string splits on dot spaces commas etc…
Then I change to colors so that it’s like the script editor, after that the comma’s and dots are gone, can someone help me keep them?

This my function right now:

local wordColors = {
	["local"] = {248, 109, 124},
	["Instance"] = {132, 214, 247},["new"] = {132, 214, 247},["Vector3"] = {132, 214, 247},["UDmi2"] = {132, 214, 247},
	["true"] = {255, 198, 0},["false"] = {255, 198, 0},
	["0"] = {255, 198, 0},["1"] = {255, 198, 0},["2"] = {255, 198, 0},["3"] = {255, 198, 0},["4"] = {255, 198, 0},["5"] = {255, 198, 0},["6"] = {255, 198, 0},["7"] = {255, 198, 0},["8"] = {255, 198, 0},["9"] = {255, 198, 0},
}

local delims = {",", " ", ".",":"}

function multiSplit(String, delims)
	local Table = {}
	local p = "[^"..table.concat(delims).."]+"
	for Word in String:gmatch(p) do
		table.insert(Table, Word)
	end	
	return Table
end

local function update()
	currentText = promptTextBox.Text
	
	local words = multiSplit(promptTextBox.Text, delims)
	
	for i, word in next, words, nil do
		if not wordColors[word] then continue end
		local color = wordColors[word]
		words[i] = string.format(
			'<font color = "rgb(%d, %d, %d)">'..word..'</font>',
			color[1],
			color[2],
			color[3]
		)
	end

	promptTextBox.Text = table.concat(words, " ")
end

The periods and spaces aren’t in “wordColors” so it just skips over them and doesn’t format the string, if I had to guess, also when you use string.split it removes the split point. So if you were to split

local hello = World
you’d get
{"local", "hello", "=", "World"}

The table doesn’t haven the spaces since I’m splitting them down the spaces. Aka. it automatically deletes the delemitters. So you could either make a custom function that splits the string and keeps the delemitters, like this

– CHAT GPT

function splitWithDelimiter(inputStr, delimiter)
    local result = {}
    local pattern = "(.-)(" .. delimiter .. ")"
    local lastEnd = 1
    local s, e, cap, delim = inputStr:find(pattern, 1)

    while s do
        if s ~= 1 or cap ~= "" then
            table.insert(result, cap)
        end
        table.insert(result, delim)
        lastEnd = e + 1
        s, e, cap, delim = inputStr:find(pattern, lastEnd)
    end

    if lastEnd <= #inputStr then
        table.insert(result, inputStr:sub(lastEnd))
    end

    return result
end

or you can add some logic to add periods and spaces between certain words.