You can use
local new_string = my_string:gsub('^%[(.*)]$', '%1')
See this Lua demo. The ^%[(.*)]$
pattern matches
^
- start of string
%[
- a literal [
char
(.*)
- captures any zero or more chars into Group 1 (%1
refers to this value from the replacement pattern)
]
- a ]
char
$
- end of string.
Alternatively, you can use
local new_string = string.gsub(my_string:gsub('^%[', ''), ']$', '')
See this Lua demo online.
The ^%[
pattern matches a [
at the start of the string and ]$
matches the ]
char at the end of the string.
If there is no need to check if [
and ]
positions, just use
local new_string = my_string:gsub('[][]', '')
See the Lua demo.
The [][]
pattern matches either a ]
char or a [
char.