3

Suppose I have a string like so:

the quick [color=brown]brown[/color] fox [color=green]jum[/color]p[color=yellow]e[/color]d over the lazy dog

What's a good way of going through it and putting it all in an array where each time the text is within the bounds of a [color] tag it will have that color tag around it? so the word 'jumped' would look like this:

[color=green]j[/color]
[color=green]u[/color]
[color=green]m[/color]
p
[color=yellow]e[/color]
d

Where each line is a new instance in the index.

Currently I'm attempting to do it in what I believe is a really messy way by parsing it heavily...

0

1 Answer 1

3
+50
local input_string = 'the quick [color=brown]brown[/color] fox [color=green]jum[/color]p[color=yellow]e[/color]d over the lazy dog'

;('[/color]'..input_string):gsub('(%b[])([^[]*)',
   function(tag, text)
      for c in text:gmatch'.' do
         print(tag == '[/color]' and c or tag..c..'[/color]')
      end
   end
)
Sign up to request clarification or add additional context in comments.

1 Comment

Works perfectly, could you link to some documentation or expand on how it works?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.