實現JavaScript中String對象的replaceAll()函數,string.replace函數

來源:互聯網
上載者:User

實現JavaScript中String對象的replaceAll()函數,string.replace函數

JavaScript指令碼語言中,大家對String對象使用replace()函數肯定不陌生,但是卻沒有如Java語言一般有replaceAll()方法,但是在使用中,又不可避免的會使用到,這種時候當然只有我們程式員自己來動手咯!!

/** * Author Joyce.Luo 10:19:54 prepared in 2015.01.05 * JavaScript language Methods: replaceAll(), based on the replace() method to realize * @param {} rgExp The need to replace the string or regular object * @param {} replaceText The string matching to replace the string * @return {} Returns a new string replacement */String.prototype.replaceAll = function(rgExp, replaceText){    return this.replace(new RegExp(rgExp,'gm'), replaceText);};

效果簡單明了。。。。。

聯繫我們

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