Skip to content

Instantly share code, notes, and snippets.

@pascaliske
Created March 15, 2017 10:57
Show Gist options
  • Save pascaliske/149e9bf6555c4d11317e144bfce16beb to your computer and use it in GitHub Desktop.
Save pascaliske/149e9bf6555c4d11317e144bfce16beb to your computer and use it in GitHub Desktop.
Convert dashed strings to camelcased strings
// ES5
var camelize = function(string) {
return string.replace(/-([a-z])/g, function(match) {
return match[1].toUpperCase();
});
};
// ES6 (short one-liner)
const camelize = string => string.replace(/-([a-z])/g, match => match[1].toUpperCase());
// Example
console.log('some-dashed-string'); // => "some-dashed-string"
console.log(camelize('some-dashed-string')); // => "someDashedString"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment