为什么我不能做这样的事情?Javascript RegExp as property
var obj = {
prop1:"a",
prop2:"b",
test1: new RegExp(this.prop1),
test2: new RegExp(this.prop2),
init: function(){
if(this.test1.test('apple')){
//code will be executed
}
if(this.test1.test('banana')){
//code will be executed
}
if(this.test2.test('apple')){
//code will be executed
}
if(this.test2.test('banana')){
//code will be executed
}
}
};
obj.init();
测试将始终返回true,因为它是/(?:)/
。如何解决这个问题?
正是你想做什么?你的问题是什么 ? – tnanoba
你到底想做什么?你的问题是什么 ?好友 – Neel
@Neel哦,我错过了'buddy'哈哈 – tnanoba