pen*_*ngz 8 javascript node.js
我正在 NodeJS 中使用 Express、Angular JS 和 Google App-Engine 数据存储构建一个 Web 应用程序。
我正在学习节点。我创建了一个在插入数据库之前“清理”(转义)用户输入的表单。我按照 MDN 网站上的 NodeJS 教程创建了以下代码:
//Trim and escape all inputs
req.sanitize('requester').escape();
req.sanitize('requester').trim();
req.sanitize('dataowner').escape();
req.sanitize('dataowner').trim();
req.sanitize('requested_filepath_list').escape();
req.sanitize('requested_filepath_list').trim();
Run Code Online (Sandbox Code Playgroud)
“requested_filepath_list”是 UNIX 文件路径的列表。
因此,当用户提交请求时,它会以“转义”格式存储在数据库中。
//Escaped data
/top/example/test123.txt
Run Code Online (Sandbox Code Playgroud)
问题:如何“转义”数据以用于显示目的?
//Desired output
/top/example/test123.txt
Run Code Online (Sandbox Code Playgroud)
我尝试了 unescape 函数,但它似乎不起作用,它只是返回相同的输出。
let escape_str = '/top/example/test123.txt';
let unescaped_str = unescape(escape_str);
console.log('unescaped_str: ' + unescaped_str);
//Output
unescaped_str: /top/example/test123.txt
//Desired output
/top/example/test123.txt
Run Code Online (Sandbox Code Playgroud)
我能够使用“he”库来实现这一要求。
这是一篇包含详细信息的帖子:What's the right way to Decode a string that has Special HTMLEntitys in it?
这里是图书馆。我能够使用 npm install 安装它。
https://www.npmjs.com/package/he
解决方案示例:
const he = require('he');
let escape_str = '/top/example/test123.txt';
let unescaped_str = he.decode(escape_str);
console.log('unescaped_str ' + unescaped_str);
Run Code Online (Sandbox Code Playgroud)
小智 -1
你可以试试这个:)
const querystring = require('querystring');
querystring.unescape(escape_str);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10728 次 |
| 最近记录: |