Tuesday, 14 August 2012

Replace characters in javascript

Replace characters in javascript

In server side we have "Replace" method to replace the characters in a string but on javascript replace method will be not useful in certain cases instead of that we can do for loop to replace the particular characters and get the specific output.
Example :
           var StringChar = "this is my 324o234u324t2342p234u234t"; //Your string to get replaced
         In this case i need to remove the numbers and get the filtered output.


        Replace Method in Javascript :
         var ReplacedCharacters = String.replace("0123456789"," ");
         Output :   "this is my 324o234u324t2342p234u234t"


         In this cases we can do like this :
         var ReplacedChar = "";
         var Characters = "";
         var InValidChars = "0123456789";
         for (d = 0; d < StringChar.length; d++) {
         Characters = StringChar.charAt(d);
         if (InValidChars.indexOf(Characters) == -1) {
         ReplacedChar = ReplacedChar + Characters;
         }
         the resulting output is :   ReplacedChar = this is my output

No comments:

Post a Comment

Popular posts