千位分隔符
function parseToMoney(num) {
num = parseFloat(num.toFixed(3));
let [integer, decimal] = String.prototype.split.call(num, '.');
integer = integer.replace(/\d(?=(\d{3})+$)/g, '$&,');
return integer + '.' + (decimal ? decimal : '');
}
电话号码校验
function isPhone(tel) {
var regx = /^1[34578]\d{9}$/;
return regx.test(tel);
}
邮箱验证
function isEmail(email) {
var regx = /^([a-zA-Z0-9_\-])+@([a-zA-Z0-9_\-])+(\.[a-zA-Z0-9_\-])+$/;
return regx.test(email);
}
身份证验证
function isCardNo(number) {
var regx = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
return regx.test(number);
}
字符串出现最多次数的字符
function getMaxCnt(str) {
const json = {};
const maxCnt = 0;
const maxCntChar = '';
str = str.split('').sort.join('');
for(let i = 0; i < str.length - 1, i++) {
const char = str[i];
const charCnt = str.lastindexOf(char)-i+1;
if(maxCnt < charCnt) {
maxCnt = charCnt;
maxCntChar = char;
}
i = str.lastIndexOf(char);
}
}
// 1
let a = [1, 2, 3];
a.toString = a.shift;
if (a == 1 && a == 2 && a == 3) {
console.log('ok');
}
// 2
let i = 0;
Object.defineProperty(window, 'a', {
get() {
return ++i;
}
});
if (a == 1 && a == 2 && a == 3) {
console.log('ok');
}
// 3
let a = {
i: 0,
[Symbol.toPrimitive]() {
return ++this.i;
}
};
if (a == 1 && a == 2 && a == 3) {
console.log('ok');
}