如何在 lua 中使用模式捕获和附加到 HTML body 标签

我有一个带有以下开放的 body 标签的 HTML 页面:

<body class="one two three" id="five" data-key="value">

我正在使用 lua 模式将一个 div 附加到末尾:

<body class="one two three" id="five" data-key="value"><div></div>

我该如何做到这一点?

备注: 我之前使用以下方法在 head 标签之前插入脚本:

body_filter_by_lua_block {
    replacestr = "<script></script></head>"
    ngx.arg[1] = ngx.re.sub(ngx.arg[1],"</head>", replacestr)
    return
}

因此,如果将我的 div 添加到 replacestr,那么我应该用什么替换 ngx.re.sub(ngx.arg[1],"</head>", replacestr)

点赞
用户3970701
用户3970701
使用lua的ngx.re.sub函数替换传入的第一个参数匹配到的字符串中,匹配模式为(<body[^>]*>),用第二个参数中的replacestr替换符合模式的字符串。
2017-08-06 01:45:18