我要这样一段字符串需要用正则替换掉它:
var str="method:function(a,b,c){var c={a,b};function a(){}},handler:function(){}";
想要把function都替掉成为:
var str = "method:null,handler:null";
我这个问题匹配出错主要是{}主间的内容;
//内容 document.getE().a.b();类似组件可以出现任意次;
var re=/function\s*\((\w*(\,)?)*\)\s*\{([^\{\}])*\}/gi;
console.log(regStr.match(re).length);
console.log(regStr.match(re));
console.log(regStr.replace(re,"null"));
我目前只能匹配空函数类似于:
var str="method:function(){},action:function(){}";
对于函数的{}中如果有内容我就没法判断了用(.*)会匹配出错误的结果
哪位高手出来给我解释这问题
var config = {title:'aa',onclick:function(){},items:[{button:{onclick:function(){}}}]},
handler = function(config){
for (var o in config){
if (!config.hasOwnProperty(o)) {
console.log('ignore %o => %o of %o', o, config[o], Object.prototype.toString.apply(config[o]));
continue;
}else if (config[o] && Object.prototype.toString.apply(config[o]) === '[object Function]') {
console.log('delete %o => %o of %o', o, config[o], Object.prototype.toString.apply(config[o]));
delete config[o];
}else if (Object.prototype.toString.apply(config[o]) === '[object Array]' || Object.prototype.toString.apply(config[o]) === '[object Object]') {
console.log('enter %o => %o of %o', o, config[o], Object.prototype.toString.apply(config[o]));
handler(config[o]);
console.log('return %o => %o of %o', o, config[o], Object.prototype.toString.apply(config[o]));
}else console.log('ignore %o => %o of %o', o, config[o], Object.prototype.toString.apply(config[o]));
}
};
handler(config);
/*
结果
ignore "title" => "aa" of "[object String]"
delete "onclick" => function (){} of "[object Function]"
enter "items" => [Object] of "[object Array]"
enter "0" => Object of "[object Object]"
enter "button" => Object of "[object Object]"
delete "onclick" => function (){} of "[object Function]"
return "button" => Object of "[object Object]"
return "0" => Object of "[object Object]"
return "items" => [Object] of "[object Array]"
*/
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。