Question

Basic question but I've been trying to figure out for a while with no luck.

I am processing urls and need to do a simple replacement.

I need to replace spaces with the literal string %20, but I can't seem to escape the % or the %2 which is reported as an invalid capture.

text = string.gsub(text, "%s+", '%%20')

How many % do I have to use inside gsub to escape the % sign and the %2 capture.

Was it helpful?

Solution

Seems to work for me:

> text="hello world"
> print(string.gsub(text, "%s+", '%%20'))
hello%20world   1

You'll need to show some more code and your error message.

OTHER TIPS

Alternatively, you can automatically do that with the following:

url = require("socket.url")
text = url.escape(string)

This is, of course, assuming you do have the socket library in your lua path. To be quite honest, this is the way I would go about doing anything with urls, because then you don't have to worry about converting commas into %2c or apostrophes into %27.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top