Skip to content

Instantly share code, notes, and snippets.

@sarcoma sarcoma/switch-case.js
Last active Jul 19, 2019

What would you like to do?
Switch String Case. Coverts camelCase or PascalCase to kebab-case or snake_case or any delimited string and vice versa,
const camelCaseToDelimitedString = (string, delimiter = '-') =>
string.replace(/([a-z0-9]|(?<!^)(?=[A-Z]))([A-Z])/g, '$1'+delimiter+'$2').toLowerCase();
const delimitedStringToCamelCase = (string, delimiter = '-') =>
string.replace(new RegExp(delimiter + '([a-z])', 'g'), (m, c) => c.toUpperCase());

This comment has been minimized.

Copy link
Owner Author

sarcoma commented Jul 19, 2019

 * Examples
console.log(camelCaseToDelimitedString('helloWorld', '_'));
console.log(camelCaseToDelimitedString('HelloWorld', '_'));
console.log(delimitedStringToCamelCase('hello, world', ', '));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.