[英]How to search the text between the HTML tags
我正在使用mongoJS来处理我的数据库查询。 我遇到了一个问题,字符串包含HTML标签,我正在使用正则表达式来搜索集合中的字符串。 如何通过忽略HTML标记来搜索文本?
var userInput = $scope.userInput; // value from user input
db.collections.find({'obj': {$regex: new RegExp(userInput) } }).toArray(function(err, result){
return res.json(result);
}
集合
[{_id:"34aw34d343s4", obj:"How are you?"},
{_id:"34asdfwer343s4", obj:"Are you okay?"},
{_id:"3sDaweqr43s4", obj:"Goodbye, my friend!"},
{_id:"34aw3sdfgds3s4", obj:"Do you know these are <strong>important</strong> items"}]
用户输入
these are
these
these are important
产量
[{_id:"34aw3sdfgds3s4", obj:"Do you know these are <strong>important</strong> items"}]
[{_id:"34aw3sdfgds3s4", obj:"Do you know these are <strong>important</strong> items"}]
[]
预期
[{_id:"34aw3sdfgds3s4", obj:"Do you know these are <strong>important</strong> items"}]
[{_id:"34aw3sdfgds3s4", obj:"Do you know these are <strong>important</strong> items"}]
[{_id:"34aw3sdfgds3s4", obj:"Do you know these are <strong>important</strong> items"}]
您应该在用户输入进入数据库之前清理它。 根据我对您的系统的理解,用户输入(在插入数据库之前)很可能不会被清理,并且您的站点很容易受到XSS攻击 。
我建议您使用像sanitize-html这样的库来保护您的网站免受跨站点脚本攻击以及此问题的答案。
你可以使用RegExp 测试方法:/ /these|are/.test(stringToCheckAgainst);
var testCases = ["these are", "these", "these are <strong>item</strong>"]; testCases.forEach(function(value) { document.write(/these|are/.test(value) + "\\n"); });
如果要删除html标记,请使用以下方法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.