我有一个名字列表,需要提供一个功能给用户使用通配符 * 和 ?(匹配任意字符串和单个字符)来筛选列表。为了避免语法注入(意外或故意),我知道需要清理用户输入内容,但不确定需要清理多少。那么,我需要替换用户输入中的 * 和 ? 以达到什么目的?
感谢您的选择。
var names = [...];
var userInput = field.value;
/* Replace * and ? for their equivalent in regexp */
userInput = userInput.replaceAll(...);
userInput = userInput.replaceAll(...);
/* clean the input */
userInput = userInput.replaceAll(...);
userInput = userInput.replaceAll(...);
...
var regex = new Regexp(userInput);
var matches = [];
for (name in names) {
if (regex.test(name)) {
matches.push(name);
}
}
/* Show the results */
感谢您的选择。