레이블이 replaceAll인 게시물을 표시합니다. 모든 게시물 표시
레이블이 replaceAll인 게시물을 표시합니다. 모든 게시물 표시

2011년 5월 18일 수요일

[Javascript] prototype replaceAll

자바스크립트로 replaceAll 구현하기: 프로토타입 사용

String.prototype.replaceAll = function(targetStr,replaceStr){
   thisStr = this.toString();
   var idx = thisStr.indexOf( targetStr );
   while ( idx > -1 ) {
       thisStr = thisStr.replace( targetStr, replaceStr );
       idx = thisStr.indexOf( targetStr );
   }
   return thisStr;
}