前端开发常用的30个js工具函数

  1. 获取URL参数值:
function getUrlParam(name) {
   
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)");
    var results = regex.exec(location.search);
    return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
  1. 检测是否是移动设备:
function isMobileDevice() {
   
    return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
}
  1. 检测是否是邮箱地址:
function isEmail(email) {
   
    var regex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
    return regex.test(email);
}
  1. 检测是否是手机号码:
function isPhoneNumber(phone) {
   
    var regex = /^\d{11}$/;
    return regex.test(phone);
}
  1. 获取当前时间戳:
function getCurrentTimestamp() {
   
    return Math.floor(Date.now() / 1000);
}
  1. 深拷贝对象:
function deepCopy(obj) 

你可能感兴趣的:(javascript,前端,开发语言)