Posted By

remysharp on 07/24/07


Tagged

String camelcase


Versions (?)

String.toCamelCase


 / Published in: JavaScript
 

"example_one".toCamelCase(); // => exampleOne "Example-Two".toCamelCase(); // => ExampleTwo "example-Three".toLowerCase().toCamelCase(); // => exampleTwo

// execption: BestPCCode => BestPccode

  1. String.prototype.toCamelCase = function() {
  2. return this.toString()
  3. .replace(/([A-Z]+)/g, function(m,l){
  4. return l.substr(0,1).toUpperCase() + l.toLowerCase().substr(1,l.length);
  5. })
  6. .replace(/[\-_\s](.)/g, function(m, l){
  7. return l.toUpperCase();
  8. });
  9. };

Report this snippet  

You need to login to post a comment.