Skip to main content

Convert camelCase to kebab-case and vice versa

JavaScript version

const kebabToCamel = (str) => str.replace(/-./g, (m) => m.toUpperCase()[1]);

const camelToKebab = (str) => str.replace(/([a-z0-9])([A-Z])/g, '$1-$2').toLowerCase();

TypeScript version

const kebabToCamel = (str: string): string => str.replace(/-./g, (m) => m.toUpperCase()[1]);

const camelToKebab = (str: string): string => str.replace(/([a-z0-9])([A-Z])/g, '$1-$2').toLowerCase();

Examples

kebabToCamel('background-color'); // 'backgroundColor'
camelToKebab('backgroundColor'); // 'background-color'

Comments