gpt4 book ai didi

javascript - 如何从快速验证器 escape() 函数中排除字符

转载 作者:行者123 更新时间:2023-12-02 22:06:33 25 4
gpt4 key购买 nike

我正在使用来自express-validator的检查来验证用户输入,并且想知道是否可以从测试中排除字符?我有

check("profile.about").trim().escape()

它将 HTML 中使用的任何字符转换为其等效的 HTML 实体,但如果用户输入撇号,则会导致不良结果。有没有办法从 escape() 方法中过滤掉撇号?如果没有,没有人对我如何模仿escape()并删除'有任何提示。谢谢

最佳答案

可以立即添加另一个中间件来完成此任务...

unescape-middleware.js

module.exports = (escaped, char) => function (req, res, next) {
unescapeCharacter(req.params, escaped, char);
unescapeCharacter(req.query, escaped, char);
unescapeCharacter(req.body, escaped, char);
return next();
}

function unescapeCharacter (obj, escaped, char) {
for (const key in obj) {
// Replace the escaped version with the character
obj[key] = obj[key].replace(new Regex(escaped, "g"), char);
}
}

然后像这样使用...

app.js

const { check } = require("express-validator");
const unescape = require("./path/to/unescape/middleware.js");

app.get(
"/some/route",
check("profile.about").trim().escape(),
unescape("'", "'"),
require("./path/to/router/handler.js")
);

我相信这应该可以解决问题......

关于javascript - 如何从快速验证器 escape() 函数中排除字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59696041/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com