我的筆記整理,不定時更新html
~~'1.2' // 1
~~'1' // 1
~~1.2 // 1
1.2|0 // 1
'1.2'|0 // 1
'1'|0 // 1
複製代碼
let a = 2, b = 1;
a = [b, b=a][0];
console.log(`a:${a},b:${b}`); // a:1,b:2
複製代碼
function getFileExtension(fileName) {
let arr = fileName.split('.');
return arr.length >= 2 ? arr.pop() : '';
}
console.log(getFileExtension('test')); // ''
console.log(getFileExtension('test.test.txt')); // txt
複製代碼
/((\+\d{2,4}-)?(0\d{1,3}-)\d{7,8})/
// 驗證手機和座機
/^(((\+\d{2,4}-)?(0\d{1,3}-)\d{7,8})|(1[3456789]\d{9}))$/
複製代碼
window.navigator
// 裏面包含當前設備操做系統信息 platform oscpu
複製代碼
if (year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0)) {
// 是閏年
}
複製代碼
function getDayOfYear() {
let numOfYear = 0;
const date = new Date();
const dateArr = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
const day = date.getDate();
const month = date.getMonth() + 1;
const year = date.getFullYear();
for ( let i = 1; i < month; i++) {
numOfYear += dateArr[i];
}
numOfYear += day;
if (month > 2 && (year % 4 === 0) && (year % 100 !== 0 || year % 400 === 0)) {
numOfYear += 1;
}
return numOfYear;
}
複製代碼
function deepclone(data) {
if(typeof data === 'object' && data !== null) {
return Object.prototype.toString.call(data) === '[object Array]' ? [ ...data ] : { ...data };
} else {
return data;
}
}
複製代碼
使用a
標籤正則表達式
<a href="tel:188xxxxxxxx">一鍵撥打號碼</a>
<a href="mailto:linxunzyf@gmail.com">一鍵發送郵件</a>
<a href="sms:188xxxxxxx">一鍵發送短信</a>
複製代碼
或者使用js瀏覽器
window.location.href = 'tel:18888888888';
window.location.href = 'mailto:linxunzyf@gmail.com';
window.location.href = 'sms:18888888888';
複製代碼
function generateRandomAlphaNum(len) {
let rdmString = "";
for (; rdmString.length < len; rdmString += Math.random().toString(36).substr(2));
return rdmString.substr(0, len);
}
// 或者
const RandomId = len => Math.random().toString(36).substr(3, len);
複製代碼
function getUrlParam(name) {
const reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //構造一個含有目標參數的正則表達式對象
const r = window.location.search.substr(1).match(reg); //匹配目標參數
return r !== null ? decodeURI(r[2]) : null; //返回參數值
}
// 或者
// 不兼容 IE 和 Edge Mobile
const params = new URLSearchParams(location.search.replace(/\?/ig, "")); // location.search = "?name=linxun&sex=male"
params.has("name"); // true
params.get("sex"); // "male"
// 或者
// 不兼容 IE
const getUrlParams = (url, key) => new URL(url).searchParams.get(key);
複製代碼
參考URLSearchParamsbash
// 前面補零
const FillZeroStart = (num, len) => num.toString().padStart(len, "0");
const num = FillZeroStart(111, 5);
// num => "00111"
// 後面補零
const FillZeroEnd = (num, len) => num.toString().padEnd(len, "0");
const num = FillZeroEnd(111, 5);
// num => "11100"
複製代碼
2019-08-07 更新dom
// 工具函數
const formatNumber = n => {
n = n.toString()
return n[1] ? n : '0' + n
}
複製代碼
/** * date: 須要格式化的日期 * format: 日期格式,默認爲 - */
const formatDate = (date, format = '-') => {
const year = date.getFullYear()
const month = date.getMonth() + 1
const day = date.getDate()
return [year, month, day].map(formatNumber).join(format)
}
複製代碼
/** * dayCount: 天數,可爲負數 * format: 日期格式,默認爲 - */
const getDateStr = (dayCount, format = '-') => {
const now = new Date();
now.setDate(now.getDate() + dayCount); // 獲取dayCount天后的日期
const year = now.getFullYear();
const month = now.getMonth() + 1;
const day = now.getDate();
return [year, month, day].map(formatNumber).join(format)
}
複製代碼