一、用正则验证的IP地址是否合法
checkIpAddr: function(ipaddr)
{
// console.log("ppppip="+ipaddr);
var exp=/^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/;
var reg = ipaddr.match(exp);
if(reg==null)
{
return false;//不合法
}
else
{
return true;//合法
}
},
二、 十六进制字符串转int
function hex2int(hex) {
var len = hex.length,
a = new Array(len),
code;
for (var i = 0; i < len; i++) {
code = hex.charCodeAt(i);
if (48 <= code && code < 58) {
code -= 48;
} else {
code = (code & 0xdf) - 65 + 10;
}
a[i] = code;
}
return a.reduce(function (acc, c) {
acc = 16 * acc + c;
return acc;
}, 0);
}
三、根据对象数组的某个属性值找到指定的元素
对象数组:
var datas = [ { "name": "商品房", "bianma": "11" }, { "name": "商铺", "bianma": "12" } ]
查找:filter() 方法将匹配元素集合缩减为匹配指定选择器的元素.该方法不改变原数组,返回的是筛选后满足条件的数组.
var data= datas.filter(function(item){ return item.bianma == "12"; }) console.log(data); // [{name: "商铺", bianma: "12"}]
删除该条数据:
datas.splice(datas.indexOf(data), 1)