常用的正則校驗

2022-09-05 15:12:11 字數 2251 閱讀 3936

生成指定範圍的隨機整數

const randomintegerinrange = (min, max) => math.floor(math.random() * (max - min + 1)) + min;

randomintegerinrange(0, 5);

生成指定範圍的隨機小數

const randomnumberinrange = (min, max) => math.random() * (max - min) + min;

randomnumberinrange(2, 10);

四捨五入到指定位數

const round = (n, decimals = 0) => number(`$e$`)}e-$`);

round(1.005, 2);

精確小數

const roundnum = (num, decimal) =>math.round(num * 10 ** decimal) / 10 ** decimal;const num = roundnum(1.69, 1);// num => 1.7

簡單的貨幣單位轉換

const tocurrency = (n, curr, languageformat = undefined) =>

intl.numberformat(languageformat, ).format(n);

tocurrency(123456.789, 'eur'); // €123,456.79

tocurrency(123456.789, 'usd', 'en-us'); // $123,456.79

tocurrency(123456.789, 'usd', 'fa'); // ۱۲۳٬۴۵۶٫۷۹

tocurrency(322342436423.2435, 'jpy'); // ¥322,342,436,423

隨機十六進製制顏色

const randomhexcolorcode = () => ;

randomhexcolorcode();

奇偶判斷  

const oddeven = num => !!(num & 1) ? "odd" : "even";const num = oddeven(2);// num => "even"

統計陣列成員個數

const arr = [0, 1, 1, 2, 2, 2];const count = arr.reduce((t, v) => , {});// count =>

陣列中某元素出現的次數

export function countoccurrences(arr, value)

簡單陣列交集

export const similarity = (arr1, arr2) => arr1.filter(v => arr2.includes(v));

實現千位分隔符

// 保留三位小數

parsetomoney(1234.56); // return '1,234.56'

parsetomoney(123456789); // return '123,456,789'

parsetomoney(1087654.321); // return '1,087,654.321'

function parsetomoney(num) )+$)/g, '$&,');

return integer + '.' + (decimal ? decimal : '');

}function parsetomoney(str))+$)/g;

return str.replace(re,',');

}

驗證是否是身份證

function iscardno(number) $)|(^\d$)|(^\d(\d|x|x)$)/;

return regx.test(number);

}

常用正則校驗

郵箱 param s export function isemail s test s 手機號碼 param s export function ismobile s test s 號碼 param s export function isphone s 0 9 test s url位址 param...

常用的相關正則校驗

1.正數 var reg1 1 9 d 0 d 2.正整數 var reg2 1 9 d 0 3.保留兩位小數 var reg3 1 9 d 0 d 4.驗證非漢字 var reg4 u4e00 u9fa5 5.驗證數字和橫槓 var reg5 0 9 6.驗證日期 20200101 2020090...

java常用正則校驗

public class regex 檢查 輸入 是否正確 正確格 式 012 87654321 0123 87654321 0123 7654321 param value return public boolean checktel string value d d d d 3 d 8 檢查手機...