1、字符串转换为数值
常规方法:
JavaScript code
vara = parseFloat("12");
varb = parseInt("34");
可以用
JavaScript code
vara = +("12");
varb = +("34");
2、多维数组
常规方法:
JavaScript code
varA = newArray(2);
A[0] = newArray(2);
A[1] = newArray(2);
A[0][0] = 1;
A[0][1] = 2;
A[1][0] = 2;
A[1][2] = 2;
可以利用对象的属性轻松实现
JavaScript code
varA = {};
A[[0, 0]] = 1;
A[[0, 1]] = 2;
A[[1, 0]] = 3;
A[[1, 2]] = 4;
3、遍历正则结果
常规方法:
JavaScript code
varstr = "a1b2c3d4e5f6g7h8i9j0k";
varmatch = str.match(/\d/g, str);
varmsg = [];
for(vari = 0; i < match.length; i++) {
msg.push(match[i]);
}
alert(msg);
可以利用String.replace方法
JavaScript code
varstr = "a1b2c3d4e5f6g7h8i9j0k";
varmsg = [];
str.replace(/\d/g, function() {
msg.push(arguments[0]);
});
alert(msg);
如果确需copy到一个普通数组,也可以更简单有效的实现:
JavaScript code
varstr = "a1b2c3d4e5f6g7h8i9j0k";
varmsg = str.replace(/\D/g,'').split('');
alert(msg);
整数前补0
网上搜的笨方法:
JavaScript code
functionpad(num, n) {
varlen = num.toString().length;
while(len <n){
num = "0"+ num;
len++;
}
returnnum;
}
alert(pad(3,6)); //000003
JavaScript code
functionpad(num, n) {
return(Math.pow(10,n)+num+'').substr(1); //缺点:位数长度有限,15位以内。
}
alert(pad(3,6)); //000003
JavaScript code
functionpad(num, n) {
y='00000000000000000000000000000'+num; //爱几个0就几个,自己够用就行
returny.substr(y.length-n);
}
alert(pad(3,6)); //000003
通过>>>取整
JavaScript code
vartest = 3.45;
alert(test>>>0);