Javascript 函数参数($0,$1)上的参数问题+;jquery,js?
对功能的怀疑($0,$1); //$0,$1两个参数 我的问题是这两个参数没有定义,但它包含一些数据??? 有什么能帮助你理解吗 这两个论点如何运作Javascript 函数参数($0,$1)上的参数问题+;jquery,js?,javascript,jquery,function,arguments,Javascript,Jquery,Function,Arguments,对功能的怀疑($0,$1); //$0,$1两个参数 我的问题是这两个参数没有定义,但它包含一些数据??? 有什么能帮助你理解吗 这两个论点如何运作 function strip_tags(input, allowed) { allowed = (((allowed || "") + "").toLowerCase().match(/<[a-z][a-z0-9]*>/g) || []).join(''); //console.log('----------->'
function strip_tags(input, allowed) {
allowed = (((allowed || "") + "").toLowerCase().match(/<[a-z][a-z0-9]*>/g) || []).join('');
//console.log('----------->'+allowed.join('ss'));
var tags = /<\/?([a-z][a-z0-9]*)\b[^>]*>/gi,
commentsAndPhpTags = /<!--[\s\S]*?-->|<\?(?:php)?[\s\S]*?\?>/gi;
return input.replace(commentsAndPhpTags,'').replace(tags, function ($0, $1) { // need help to understand $0 , $1
//console.log('----------->'+$1);
return allowed.indexOf('<' + $1.toLowerCase() + '>') > -1 ? $0 : '';
});
}
功能条标签(输入,允许){
allowed=((allowed | |“”)+).toLowerCase().match(//g)| |[]).join(“”);
//console.log('----------->'+allowed.join('ss');
变量标记=/]*>/gi,
commentsAndPhpTags=/|/gi;
返回input.replace(commentsAndPhpTags“”).replace(标记,函数($0,$1){//需要帮助才能理解$0,$1
//console.log('----------->'+$1);
允许返回。indexOf(“”)>-1?$0:“”;
});
}
返回输入。替换(commentsAndPhpTags.)。替换(标记,函数(输入,组1){
//console.log('----------->'+group1);
允许返回。indexOf(“”)>-1?输入:“”;
});
您的regex/]*>/gi
只包含一个组匹配项,它将是括号内的内容([a-z][a-z0-9]*)
,replace()
将原始字符串和组匹配项传递给您的函数
然而,您的正则表达式应该是这样的
/(]*>)/gi
,以便能够去除标记。这是一种非常糟糕的清除标记的方法。几乎肯定会有一些漏洞。更简单的方法是剥离所有标记:
var stripTags = function(str) {
return str.replace(/<[^>]+>/g, '');
};
var stripTags=函数(str){
返回str.replace(/]+>/g',);
};
就允许特定元素而言,最好编写一个标记器,迭代标记,删除所有不允许的内容,然后从这些标记输出标记
但如果您不想编写标记器,这将是一种更好的方法,尽管它仍然有点粗糙:
var allowed = { p: true, a: true };
var sanitize = function(str) {
return str.replace(/<\s*\/?\s*([^\s>]+)[^>]*>/g, function(tag, name) {
if (!allowed[name.toLowerCase()]) {
return '';
}
return tag;
});
};
var-allowed={p:true,a:true};
var sanitize=函数(str){
返回str.replace(/]+)[^>]*>/g,函数(标记,名称){
如果(!allowed[name.toLowerCase()]){
返回“”;
}
返回标签;
});
};
但是正如上面的评论所提到的,如果您只是在客户端清理用户的标记,这是一个主要的问题。您需要在服务器端进行清理。您是指JavaScript函数形成[lexical closures][1]还是$0和$1以某种方式绑定到您意想不到的东西?请你澄清一下你的问题好吗?[1] :如果您的客户端
strip_tags
功能是您唯一的安全预防措施,那么您就注定要失败。如果你在服务器端有一个strip tags功能,你可以把它扔掉。+1表示花时间。代码中有很多错误的地方,我不想纠正所有的错误@k6t返回:做出正确的决定,删除代码并继续使用此代码,仅用于客户端验证。
var allowed = { p: true, a: true };
var sanitize = function(str) {
return str.replace(/<\s*\/?\s*([^\s>]+)[^>]*>/g, function(tag, name) {
if (!allowed[name.toLowerCase()]) {
return '';
}
return tag;
});
};