Monday, November 21, 2011

Javascript: String function - camelize, underscore

To camel case

 function camelise(str){  
      var camelize=str.replace (/(?:^|[-_])(\w)/g, function (_, c) {  
       return c ? c.toUpperCase () : '';  
      })  
      return camelize;  
 }  


To Underscore from Camel Case
 function underscore(str){  
       var txt=str.replace(/([A-Z])/g, function($1){return "_"+$1.toLowerCase();});  
       txt=txt.substr(1);  
       return txt;  
 }  

No comments:

Post a Comment