javascript_core之正則、Math、Date

來源:互聯網
上載者:User

標籤:image   title   四捨五入   正則   自動   block   tom   tar   set   

1、RegExp:Regular Expression,建立封裝Regex:

    ①正則直接量:var reg=/reg/ig;②var reg=new RegExp(“reg”,“ig”);

2、RegExp API:

    ①找每個關鍵詞,並獲得位置:var arr=reg.exec(str);預設從頭開始尋找;

        傳回值:arr:【“關鍵詞”,1,1,2,$3……】;

            arr.index:本次找到關鍵詞的位置;

        尋找:reg.lastIndex:下次開始的位置;

            RegExp.$n:本次關鍵詞的第n個分組;

3、驗證:var bool=reg.test(str);返回布爾值;

4、Math:

    ①上取整:Math.ceil(num);②下取整:Math.floor(num);③四捨五入取整:Math.round(num);④按任意位小數四捨五入:n.toFixed(d);⑤乘方:Math.pow(底數,冪);⑥開平方:Math.sqrt(num);⑦最大值:Math.max(值1,值2……);⑧最小值:Math.min(值1,值2……);⑨擷取數組中的最大值:Math.max.apply(null,arr);10擷取數組中的最小值:Math.min.apply(null,arr);11隨機數:Math.random();產生0~1之間的小數;12隨機數:parseInt(Math.random()*(max-min+1)+min);

5、Date:封裝操作時間API,從1970年1月1日至今的毫秒數;

    建立:①建立日期對象,自動擷取用戶端系統時間:var now=new Date();②建立日期對象,並自訂時間:var date=new Date(“year/month/date hours:minutes:seconds”);或者var date=new Date(year,month-1,date,hours,minutes,second);③複製日期對象:var date2=new Date(date1);

6、Date API:

    ①分量:FullYear,Month,Date,Day(星期),Hours,Minutes,Seconds,Milliseconds;

    ②方法:getXXX:擷取指定分量的值;setXXX修改指定分量的值;

    ③計算:日期相減結果為毫秒數;date.setXXX(date.getXXX()+/-n);

    ④轉字串:

將date轉為國際標準日期格式:date.toString();

轉為當地時間格式:date.toLocaleString();

僅保留日期:date.toLocaleDateString();

僅保留時間:date.toLocaleTimeString();

此文轉載 地址:http://www.cnblogs.com/Jupiter258/

   

javascript_core之正則、Math、Date

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.