千位分隔符

  1. function parseToMoney(num) {
  2. num = parseFloat(num.toFixed(3));
  3. let [integer, decimal] = String.prototype.split.call(num, '.');
  4. integer = integer.replace(/\d(?=(\d{3})+$)/g, '$&,');
  5. return integer + '.' + (decimal ? decimal : '');
  6. }

电话号码校验

  1. function isPhone(tel) {
  2. var regx = /^1[34578]\d{9}$/;
  3. return regx.test(tel);
  4. }

邮箱验证

  1. function isEmail(email) {
  2. var regx = /^([a-zA-Z0-9_\-])+@([a-zA-Z0-9_\-])+(\.[a-zA-Z0-9_\-])+$/;
  3. return regx.test(email);
  4. }

身份证验证

  1. function isCardNo(number) {
  2. var regx = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
  3. return regx.test(number);
  4. }

字符串出现最多次数的字符

  1. function getMaxCnt(str) {
  2. const json = {};
  3. const maxCnt = 0;
  4. const maxCntChar = '';
  5. str = str.split('').sort.join('');
  6. for(let i = 0; i < str.length - 1, i++) {
  7. const char = str[i];
  8. const charCnt = str.lastindexOf(char)-i+1;
  9. if(maxCnt < charCnt) {
  10. maxCnt = charCnt;
  11. maxCntChar = char;
  12. }
  13. i = str.lastIndexOf(char);
  14. }
  15. }
  1. // 1
  2. let a = [1, 2, 3];
  3. a.toString = a.shift;
  4. if (a == 1 && a == 2 && a == 3) {
  5. console.log('ok');
  6. }
  7. // 2
  8. let i = 0;
  9. Object.defineProperty(window, 'a', {
  10. get() {
  11. return ++i;
  12. }
  13. });
  14. if (a == 1 && a == 2 && a == 3) {
  15. console.log('ok');
  16. }
  17. // 3
  18. let a = {
  19. i: 0,
  20. [Symbol.toPrimitive]() {
  21. return ++this.i;
  22. }
  23. };
  24. if (a == 1 && a == 2 && a == 3) {
  25. console.log('ok');
  26. }