JavaScript 有用的代码片段和 trick(2)
注:更多方法请参考《How to flatten nested array in JavaScript?》
日期格式化
// 方法一 function format1(x, y) { var z = { y: x.getFullYear(), M: x.getMonth() + 1, d: x.getDate(), h: x.getHours(), m: x.getMinutes(), s: x.getSeconds() }; return y.replace(/(y+|M+|d+|h+|m+|s+)/g, function(v) { return ((v.length > 1 ? "0" : "") + eval('z.' + v.slice(-1))).slice(-(v.length > 2 ? v.length : 2)) }); } format1(new Date(), 'yy-M-d h:m:s'); // 17-10-14 22:14:41 // 方法二 Date.prototype.format = function (fmt) { var o = { "M+": this.getMonth() + 1, //月份 "d+": this.getDate(), //日 "h+": this.getHours(), //小时 "m+": this.getMinutes(), //分 "s+": this.getSeconds(), //秒 "q+": Math.floor((this.getMonth() + 3) / 3), //季度 "S": this.getMilliseconds() //毫秒 }; if (/(y+)/.test(fmt)){ fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length)); } for (var k in o){ if (new RegExp("(" + k + ")").test(fmt)){ fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length))); } } return fmt; } new Date().format('yy-M-d h:m:s'); // 17-10-14 22:18:17
特殊字符转义
function htmlspecialchars (str) { var str = str.toString().replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, '"'); return str; } htmlspecialchars('&jfkds<>'); // "&jfkds<>"
动态插入js
function injectScript(src) { var s, t; s = document.createElement('script'); s.type = 'text/javascript'; s.async = true; s.src = src; t = document.getElementsByTagName('script')[0]; t.parentNode.insertBefore(s, t); }
格式化数量
// 方法一 function formatNum (num, n) { if (typeof num == "number") { num = String(num.toFixed(n || 0)); var re = /(-?\d+)(\d{3})/; while (re.test(num)) num = num.replace(re, "$1,$2"); return num; } return num; } formatNum(2313123, 3); // "2,313,123.000" // 方法二 '2313123'.replace(/\B(?=(\d{3})+(?!\d))/g, ','); // "2,313,123" // 方法三 function formatNum(str) { return str.split('').reverse().reduce((prev, next, index) => { return ((index % 3) ? next : (next + ',')) + prev }); } formatNum('2313323'); // "2,313,323"
身份证验证
function chechCHNCardId(sNo) { if (!this.regExpTest(sNo, /^[0-9]{17}[X0-9]$/)) { return false; } sNo = sNo.toString(); var a, b, c; a = parseInt(sNo.substr(0, 1)) * 7 + parseInt(sNo.substr(1, 1)) * 9 + parseInt(sNo.substr(2, 1)) * 10; a = a + parseInt(sNo.substr(3, 1)) * 5 + parseInt(sNo.substr(4, 1)) * 8 + parseInt(sNo.substr(5, 1)) * 4; a = a + parseInt(sNo.substr(6, 1)) * 2 + parseInt(sNo.substr(7, 1)) * 1 + parseInt(sNo.substr(8, 1)) * 6; a = a + parseInt(sNo.substr(9, 1)) * 3 + parseInt(sNo.substr(10, 1)) * 7 + parseInt(sNo.substr(11, 1)) * 9; a = a + parseInt(sNo.substr(12, 1)) * 10 + parseInt(sNo.substr(13, 1)) * 5 + parseInt(sNo.substr(14, 1)) * 8; a = a + parseInt(sNo.substr(15, 1)) * 4 + parseInt(sNo.substr(16, 1)) * 2; b = a % 11; if (b == 2) { c = sNo.substr(17, 1).toUpperCase(); } else { c = parseInt(sNo.substr(17, 1)); } switch (b) { case 0: if (c != 1) { return false; } break; case 1: if (c != 0) { return false; } break; case 2: if (c != "X") { return false; } break; case 3: if (c != 9) { return false; } break; case 4: if (c != 8) { return false; } break; case 5: if (c != 7) { return false; } break; case 6: if (c != 6) { return false; } break; case 7: if (c != 5) { return false; } break; case 8: if (c != 4) { return false; } break; case 9: if (c != 3) { return false; } break; case 10: if (c != 2) { return false; }; } return true; }
内容版权声明:除非注明,否则皆为本站原创文章。