Development/Javascript

replaceArray - 배열간 문자열 탐색변경

Dev. Jkun 2015. 4. 27. 17:26
반응형

출처 : http://stackoverflow.com/questions/5069464/replace-multiple-strings-at-once

 String.prototype.replaceArray = function (find, replace) {
     var replaceString = this;
     for (var i = 0; i < find.length; i++) {
         // global replacement
         var pos = replaceString.indexOf(find[i]);
         while (pos > -1) {
             replaceString = replaceString.replace(find[i], replace[i]);
             pos = replaceString.indexOf(find[i]);
         }
     }
     return replaceString;
 };
     var textT = "Hello world,,,,, hello people.....";
     var find = [".",","];
     var replace = ['2', '5'];
     textT = textT.replaceArray(find, replace);


반응형