How to ignore all richtext markups when printing .Text?

I have this function that allows all buttons with a tag to do stuff on mouse hover. At the moment, it works on most buttons. But the problem is, if the text changes on a certain button, while my mouse is hovered over, and i move the mouse off, it resets back to what it was before. So I want the text to basically be the unbolded version of the current button.Label.Text, however I can’t just do button.Label.Text == button.Label.Text, as it’ll include the bold markups. How can I remove these markups and just get the actual text?

local function NewButton(button)
	local DefaultText = button.Label.Text
	
	--// Mouse entered button
	button.MouseEnter:Connect(function()
		DefaultText = button.Label.Text
		
		button.Label.Text = "<b>" .. DefaultText .. "</b>"
		button.UIGradient.Enabled = false
	end)
	
	--// Mouse leave button
	button.MouseLeave:Connect(function()
		button.Label.Text = DefaultText
		button.UIGradient.Enabled = true
	end)
end

Perhaps you could create a function you could use to extract the message part of the string?

local function getTextContent(text)
	local firstIndex = text:find(">")

	if not firstIndex then return text end

	local lastIndex = text:find("<", firstIndex)

	return text:sub(firstIndex + 1, lastIndex - 1)
end

print(getTextContent("<b>boldified text</b>"))

--<< boldified text

Also not sure how many formatting tags you’re using, I built this to work with just one pair.

Hope it helps though

1 Like

Does this help your case?

local function removeTags(str)
	-- replace line break tags (otherwise grapheme loop will miss those linebreak characters)
	str = str:gsub("<br%s*/>", "\n")
	return (str:gsub("<[^<>]->", ""))
end

taken from:

3 Likes