前端鄙视题

1、给定一个只包括'(' , ')' , '[' , ']' , '{' , '}' 的字符串,判断字符串是否有效。

 

function aa(str){
    if(Object.prototype.toString.call(str)!="[object String]" || str.length%2 != 0) {
        return false
    }
    let s = str,count=0;
    for(var i=0; i<str.length/2; i++){
        s = s.replace(/\[\]/g,'').replace(/\(\)/g,'').replace(/\{\}/g,'')
        count++
    }
    if(s.length>0){
        return false
    } else {
        return true
    }
}
aa('({})[]')  // true

 

posted @ 2022-01-20 10:26  lcspring  阅读(53)  评论(0编辑  收藏  举报