I was wondering how I could remove RichText tags. Why? I’ve made a custom chat that uses RichText and supports bubble chat, but sadly bubble chat doesn’t use RichText, so I’ve made a small function here, as of right now, it doesn’t work. I’ve read this article about string patterns, but I quite can’t nitpick how it works exactly.
local function removeTags(str)
return str:gsub("<.+>", "") -- ".+" as for like any character more than 1 character.
end -- "<" and ">" is just characters that resemble the starting and ending of a tag, e.g. "<font color=foobar>"
Once I call the function with an argument which is a string: "<b>something</b>", I get "", as like an empty string in return.
What am I doing wrong here?
So <.+> finds the character < then match everything until >, because > at the end of the string is the last match of >.
You can use [^>] so it won’t match the > character <[^>]+>
or alternatively you can use the - (the lazy quantifier) to match few character as possible (so it’ll match anything until the first >) <.-> (If you’re on RegEx, the equivalent would be <.+?>)
Thanks, that helps! But what if I would want to keep <, as of right now, it just disappears.
I want this function to hide real RichText tags, not either of these: < >
I’ve set up a simple function that converts it from < > & " ’ to escape forms with gsub, don’t worry about that.
Players can chat with these symbols without any risk of messing things up.
You can try to check if there’s a backslash before the < character: (\\?)<[^<>]->
then set the match to a table that checks if it has \< so it won’t remove it.
Here’s an example:
local function removeTags(str)
return (str:gsub("(\\?)<[^<>]->", { [''] = '' }))
end
print(removeTags("\<<b>test</b>\>")) --> <test>
As a table is inserted in string.gsub, it gets tried to get the index of the first match (in this instance, it’s either '' or '\\'), if that value is false(?) or nil, it’ll return the entire string that has been matched.
Thank you! Much respects, because you’ve been typing for so long…
This works really well, but what about checking if there’s anything inside the symbols? Someone might probably say <> for aesthetics for advertising, etc. e.g. “<> Check out our shop! <>”
Examples:
“<>” = “<>” / “<a>” = “”
Perhaps maybe checking for any valid RichText tags instead?
“<>” = “<>” / “<a>” = “<a>” / “<b>” = “”, etc.
Edit: I realized that I was “fixing” the RichText symbols, written by a player to be " or something, so first, I do the gsub with the raw RichText string sent from the server, and then “unfix” the string, so first the tags are removed, while the " & etc. are still there, and then unfix, so it’s changing these escape forms to be normal symbols.
Problem solved!
I’ve got one other question, how could I replace a string and keep some of its contents?
e.g. "*teh*" > "<b>teh</b>"
I’m at this point:
local function foobar(str)
return str:gsub("**.-**", "<b>.</b>") -- This just returns dots? :c
end -- Now I've just realized, making string patterns in the 2nd argument won't work.