JS正則匹配實例郵箱、手機號、電話、貨幣


1. 千位符:

function formatMoney (number, places, symbol, thousand, decimal){
  number = number || 0;
  //保留的小位數 可以寫成 formatMoney(542986,3) 后面的是保留的小位數,否則默 認保留兩位
  places = !isNaN(places = Math.abs(places)) ? places : 2;
  //symbol表示前面表示的標志是¥ 可以寫成 formatMoney(542986,2,"$")
  symbol = symbol !== undefined ? symbol : "¥";
  //thousand表示每幾位用,隔開,是貨幣標識
  thousand = thousand || ",";
  //decimal表示小數點
  decimal = decimal || ".";
  //negative表示如果錢是負數有就顯示“-”如果不是負數 就不顯示負號
  //i表示處理過的純數字
  var negative = number < 0 ? "-" : "";
  var i = parseInt(number = Math.abs(+number || 0), 10) + "";
  console.log(i);
  var j = (j = i.length) > 3 ? j % 3 : 0;
  return symbol + negative + (j ? i.substr(0, j) + thousand : "") + i.substr(j).replace(/\B(?=(\d{3})+(?!\d))/g, thousand) + (places ? decimal + Math.abs(number - i).toFixed(places).slice(2) : "");
}
console.log(formatMoney(1234567.1234,3));

https://juejin.im/post/5b026bbb5188256720345bb4

正則表達式的詳解。

1-2. JavaScriptAPI。

var formatter = new Intl.NumberFormat('en-US', {
  style: 'currency',
  currency: 'JPY',   //此處為各貨幣的縮寫,如$USD、¥JPY,CAD,EUR,GBP等,都能展示對應的貨幣顯示
  minimumFractionDigits: 2
});

console.log(formatter.format(12345.5556))//¥12,345.56

2. 

/*是否帶有小數*/
function    isDecimal(strValue )  {  
   var  objRegExp= /^\d+\.\d+$/;
   return  objRegExp.test(strValue);  
}  

/*校驗是否中文名稱組成 */
function ischina(str) {
    var reg=/^[\u4E00-\u9FA5]{2,4}$/;   /*定義驗證表達式*/
    return reg.test(str);     /*進行驗證*/
}

/*校驗是否全由8位數字組成 */
function isStudentNo(str) {
    var reg=/^[0-9]{8}$/;   /*定義驗證表達式*/
    return reg.test(str);     /*進行驗證*/
}

/*校驗電話碼格式 */
function isTelCode(str) {
    var reg= /^((0\d{2,3}-\d{7,8})|(1[3584]\d{9}))$/;
    return reg.test(str);
}

/*校驗郵件地址是否合法 */
function IsEmail(str) {
    var reg=/^\w+@[a-zA-Z0-9]{2,10}(?:\.[a-z]{2,4}){1,3}$/;
    return reg.test(str);
}

var data = 'windows 98 is ok';

data.match(/windows (?=\d+)/);   // ["windows "]
data.match(/windows (?:\d+)/);   // ["windows 98"]
data.match(/windows (\d+)/);     // ["windows 98", "98"]


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM