js中用到的方法

來源:互聯網
上載者:User

標籤:中介   bsp   world   理解   位置   log   ace   方法   字元   

在學習js基礎,有時候學習過就忘了,寫下來便於下次尋找方便和理解:

.substring() 方法用於提取字串中介於兩個指定下標之間的字元。

var str=‘abcdef‘;alert(str.substring(2, 5));    //從第2位開始(包含弟2個)到第4位結束(不包括第5個) 彈出cde//alert(str.substring(1));//從第1位開始到最後 彈出bcdef

.charAt() 方法可返回指定位置的字元

var str=‘abcdef‘;alert(str.charAt(0));//輸出是a

.search() 方法用於檢索字串中指定的子字串,或檢索與Regex相匹配的子字串

var str=‘abcdef‘;alert(str.search(‘b‘));      //輸出 1 位置, 1alert(str.search(‘u‘));        //位置, -1

.split() 方法用於把一個字串分割成字串數組。

var str=‘12-56-aaa-89‘;var arr=str.split(‘-‘); //輸出12,56,aaa,89

.replace() 方法用於在字串中用一些字元替換另一些字元,或替換一個與Regex匹配的子串。

var str=‘abc aaa erw‘;var re=/a/g;alert(str.replace(re, ‘0‘));//用0替換所有的a

.match() 方法可在字串內檢索指定的值,或找到一個或多個Regex的匹配。

var str=‘asdf 34 656 cvs33‘;var re=/\d/g;alert(str.match(re));//輸出3,4,6,5,6,3,3//var str="Hello world!"//document.write(str.match("world") + "<br />") //world

 

js中用到的方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.