How to get text in brackets from string

i know, im aware that the title is confusing to understand because my english is not good. so here i will try my best to explain.

here i have a text label that contain a string [Hello] [World]

I want to make that the [Hello] [World] string to a table

local table = {
   [1] = 'Hello',
   [2] = 'World',
}

all help is appreciated!

1 Like

I admit it, I used ChatGPT, but it was very straight forward so decided to reply with it anyway:

function fetchBracketText(str: string): {string}
	local texts = {}
	for text in str:gmatch("%[(.-)%]") do
		table.insert(texts, text)
	end
	return texts
end

local texts = fetchBracketText("this [is] a [test]")
print(texts) --{"is", "test"}
2 Likes

lemme try it, thank you btw for the help

thanks for your help, it works appreciate that!

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