Remove everything between [] (And also [])

For example: [daoisdasid]text
It should be text

You can use this:

local text = "[daoisdasid]text"
local newString = string.gsub(text, "%[.+]", "")
print(newString) -- "text"

string.gsub takes a string ("[daoisdasid]text"), then uses a string pattern ("%[.+]") and replaces it with a provided string ("").

Explanation of the string pattern used in this solution ("%[.+]"):

  • % signals the start of a string pattern
  • [ matches this specific character
  • . matches any character
  • + matches the maximum number of [class] characters [SEE BELOW]
  • ] matches this specific character

Explanation for “+”:

This character matches the maximum number of characters in a [class]:

%l+ matches the maximum number of lower case characters, or until the next provided pattern:

--// No "+"
print(string.match("abcDEF", "%l")) --> "a"

--// With "+"
print(string.match("abcDEF", "%l+")) --> "abc"

--// Next pattern, "x", provided
print(string.match("uvwxyz", "%l+x")) --> "uvwx"

In this case, .+] matches any character until it finds ].

1 Like