How to add custom highlighted text

I want to turn this:

s = 'I need you to <h>Listen</h> to me'

To this:

s = 'I need you to <font color="#FF7800">Listen</font> to me'

Currently I have this overcomplicated code that I believe could be a lot better

function replace_char3(pos, endpos, str, r) 
	return table.concat{str:sub(1,pos-1), r, str:sub(endpos+5)}
end

local promptText = "I need you to <h>Listen</h> to me"
local capturePhrase = string.match(promptText, '<h>(.*)</h>')
promptText = promptText:gsub("<h>", "")

local startIndexOfPhrase, endIndex = string.find(promptText, capturePhrase)
capturePhrase = '<font color="#FF7800">' .. capturePhrase .. '</font>'
promptText = replace_char3(startIndexOfPhrase, endIndex, promptText, capturePhrase)

print(promptText) -- 'I need you to <font color="#FF7800">Listen</font> to me'

The “replace_char3” function reference:

1 Like
local promptText = "I need you to <h>Listen</h> to me"
local newText = promptText:gsub("<h>(.+)</h>", '<font color="#FF7800">%1</font>')
print(newText)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.