美文网首页
JavaScript

JavaScript

作者: 百炼 | 来源:发表于2017-09-26 08:38 被阅读0次

正则表达式过滤关键字

// var ze = "<(\S*?) [^>]*>.*?</\1>|<.*? />";
// console.log(ze.test())
function filterKey(str) {
    var patternHtml = new RegExp("<(\S*?) [^>]*>.*?</\1>|<.*? />", "i");
    // console.log(patternHtml.test(str)); //输出 false 
    //百分号不过滤(下文去掉%)
    // var pattern = /[\;(--)%+<>()\"\']/;
    var pattern = /[\;+<>()\"\']/;
    //双减号
    var paternSub = /[-]{2}/
    var flag = (!patternHtml.test(str)) && (!pattern.test(str) && (!paternSub.test(str)));
    var strLower = str.toString().toLowerCase();
    var hasScriptTag = strLower.includes("script") || strLower.includes("alert");
    return flag && (!hasScriptTag);
}

// //过滤script alert meta等标签
// var strLower = str.toString().toLowerCase();
// var hasScriptTag = strLower.includes("script") || strLower.includes("alert");
// return flag&&hasScriptTag;
//过滤script alert onload等标签
//
// function checkIfHasScriptTag(str) {
//     var strLower = str.toString().toLowerCase();
//     return strLower.includes("script") || strLower.includes("alert");
// }
//script alert onload等标签

// var str = "<script></SCRIPT>";   
// // var reg = /<(.*)>.*<\/\1>|<(.*) \/>/i;
// //HTML标签


function test() {
    var arrStr = ["basic", "cninfo.html?s=finalpage%2F2017-09-22%2Fchinext1203994161.js","alert", "script", "<script></script>", "<meta>", "<alert>", "<h1>", "<a>", "(", ")", "<", ">", "%", "-", "--", "'", "a href=", "finalPage/2017-09-16/chinext123156.js"];
    for (var idx in arrStr) {
        console.log(arrStr[idx] + "  :  " + filterKey([arrStr[idx]]));
    }
}

test();
console.log("------")
reg = new RegExp(/[-]{2}/)
console.log(reg.test("www"))
console.log(reg.test("--235"))

console.log("------")
console.log('Blue Whale'.toLocaleLowerCase().includes('blue'));


// console.log(); //输出 false
test();
console.log("------")
reg = new RegExp(/[-]{2}/)
console.log(reg.test("www"))
console.log(reg.test("--235"))

console.log("------")
console.log('Blue Whale'.toLocaleLowerCase().includes('blue'));

相关文章

网友评论

      本文标题:JavaScript

      本文链接:https://www.haomeiwen.com/subject/iogjextx.html