I’ve been trying to write a gsub that converts a string with punctuation, e.g.
Built-In/Roact
Into:
Built%-In%/Roact
I can isolate the punctuation and special characters with [%c%p]
but I don’t know how to replace it with itself but with a character infront of it.
2 Likes
realmile
(mile)
#2
You can put a function as third argument of the gsub which will get called with a string that matched the pattern and needs to return a string:
string.gsub(str,"[%p%c]+",function (m) return "%"..m end)
2 Likes
I’m not sure about other nifty tricks regarding gsub but to reference itself is %1.
So in your case,
("Built-In/Roact"):gsub("[%c%p]", "%%%1")
| "%%" = "%"
and "%1"
is the captured result for the provided pattern.
I don’t really know how to word this as I’ve only seen it in a script before and it’s nice for adding radix’s for numbers.
3 Likes
Thanks! This works, but @Kitsomia’s answer is more concise so I will use that.
system
(system)
Closed
#5
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.