在使用到正则表达式,进行字符串获取的时候,发现数据返回为空,那么怎么处理,呢?
需要引用处理为空的情况,
// var a = "";
// var a = " ";
// var a = null;
// var a = undefined;
// var a = [];
// var a = {};
// var a = NaN;
if(a === undefined) { // 只能用 === 运算来测试某个值是否是未定义的
console.log("为undefined");
}
if(a == null) { // 等同于 a === undefined || a === null
console.log("为null");
}
// String
if(a == "" || a == null || a == undefined){ // "",null,undefined
console.log("为空");
}
if(!a){ // "",null,undefined,NaN
console.log("为空");
}
if(!$.trim(a)){ // "",null,undefined
console.log("为空");
}
// Array
if(a.length == 0){ // "",[]
console.log("为空");
}
if(!a.length){ // "",[]
console.log("为空");
}
// Object {}
if($.isEmptyObject(a)){ // 普通对象使用 for...in 判断,有 key 即为 false
console.log("为空");
}
具体使用到的情况如下:
var aa=/abcdefge:.*<td>(.*)</
var dic_a={
aa:'',
}
//如果没有匹配到对应的数值,那么就进行赋值操作。
var a=[aa]
var b=['aa']
for(i=0;i<a.length;i++){
if(StringNeedtoCompoare.match(a[i]) === null){
dic_a[b[i]]='NA'
} else{
if(StringNeedtoCompoare.match(a[i]).length>1){
dic_a[b[i]]=StringNeedtoCompoare.match(a[i])[1]
}
}
}