JavaScript中一些常用的方法整理

当前时间和输入时间比较

var timeLong = Date.parse(new Date());//当前时间
var t1 = Date.parse($("#returnTime2").val().replace(/-/g, "/"));

timeLong是通过js获取当前时间

t1 火狐不支持2012-1-1这种格式,所以需要转换一下。

替换所有要替换的文字 replaceAll
String.prototype.replaceAll = function (str1, str2) {
var str = this;
var result = str.replace(eval("/" + str1 + "/gi"), str2);
return result;
}

正则表达式整理

return !/^[0-9]+.?[0-9]*$/.test(val);  判断时候是正数,1、12.3 这种满足条件

return str.replace(/(^s*)|(s*$)/g, ""); 移除掉输入文本两端的空字符

原文地址:https://www.cnblogs.com/ansheng/p/6032484.html