Posted By

szsk on 08/02/06


Tagged

javascript String


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

gfazioli


string_camelize


 / Published in: JavaScript
 

  1. String.prototype.camelize = function( ) {
  2. return this.replace( /-([a-z])/g,
  3. function( $0, $1 ) { return $1.toUpperCase( ) } );
  4. }
  5.  
  6. alert( "get-element-by-id".camelize( ) );
  7. //getElementById

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: KenCorbettJr on April 11, 2012

It could also be done without modifying the prototype like this: function camelize(str) { return (str + "").replace(/-\D/g, function(match) { return match.charAt(1).toUpperCase(); }); }

You need to login to post a comment.