Help with string.gsub

Hello everyone! :wave:
While working on my project I’ve encountered a problem: I need, in a string, to add a space after each capital letter, but I don’t know how to do it.

What I want to achieve is:

"MyStringOfText" == "My String Of Text"

I’ve tryied using string.gsub, but since gsub replaces a letter I got this:

"MyStringOfText" == " y tring f ext"

So my question is:
Is there any way to refer to the letter replaced by string.gsub?
For example, referring to the prevoius example, is there any way to refer to the capital letters that got replaced by the spaces?

Thanks in advance! :smiley:

Are you looking for the function argument inside the gsub?

local function example(v)
   print(v);
end;
print(("example"):gsub('(%a)', example));

or maybe be the %1, %2, etc syntax?

print(("MyStringOfText"):gsub('[A-Z]', ' %1')) --> My String Of Text
1 Like

Yes, that’s actually what I was looking for. Thanks!