我有一个对象,我想测试属性的名称,而不是内容而是名称。我无法实现它:
theCtns = {
"services": {
"a": {
"image": "a"
},
"b": {
"image": "b"
}
}
alreadyExistSrv = false;
regex = new RegExp('(^|\\s)' + "b" + '(?=\\s|$)', 'g');
for (var service in theCtns.services) {
if (regex.test(theCtns.services[service])){
alreadyExistSrv = true;
}
}
if(alreadyExistSrv){
console.log("the name is free");
}else{
console.log("the name already exists");
}
alreadyExistSrv = false;
regex = new RegExp('(^|\\s)' + "bb" + '(?=\\s|$)', 'g');
for (var service in theCtns.services) {
if (regex.test(theCtns.services[service])){
alreadyExistSrv = true;
}
}
if(alreadyExistSrv){
console.log("the name is free");
}else{
console.log("the name already exists");
}
因此,当我在正则表达式中使用"b"
时,它应该说the name already exists
,当我使用"bb"
时,它应该说the name is free