I want to parse out the id and name values in an HTML form like so:
<input type="text" id="uid" name="uid" value="" style="width: 150px;">
and replace them with alternative names via ngx.re.sub. My issue is, when I don't use quotes to anchor the substitution in ngx.re.sub() the wrong HTML text gets substituted however sometimes the HTML uses double quotes and sometimes single quotes. My questions are as follows:
1. Is there a better way to parse HTML in openresty/lua?
2. If regexes are the way to go then I use the following code. When I tried capturing whether the HTML used single or double quotes and injecting that into my ngx.re.sub regex, the substitution no longer worked. How can I capture the quotes and include them in the regex?
local it, err = ngx.re.gmatch(chunk, "id=[\'\"]([^%\'\"]+)[\'\"] name=[\'\"]([^%\'\"]+)[\'\"]")
while true do
local m, err = it()
if err then
ngx.log(ngx.ERR, "error: ", err)
return
end
if not m then
-- no match found (any more)
break
end
-- Update ID and Name variables
UpdatedIDVar = UpdateIDVar(m[1])
UpdatedNameVar = UpdateNameVar(m[2])
ngx.arg[1] = ngx.re.sub(ngx.arg[1], m[1], UpdateIDVar)
ngx.arg[1] = ngx.re.sub(ngx.arg[1], m[2], UpdatedNameVar)
end