Skip to content

Instantly share code, notes, and snippets.

@fzn0x
Created April 17, 2022 22:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fzn0x/f79dd3ab606520ee047a68fc6f1cd4a9 to your computer and use it in GitHub Desktop.
Save fzn0x/f79dd3ab606520ee047a68fc6f1cd4a9 to your computer and use it in GitHub Desktop.
Convert camelCase to snakeCase without including capital-only word, first capital word like replace(/([A-Z])/g, "_$1").toLowerCase(); to lower case.
const toSnakeCase = str =>
str &&
str
.match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g)
.map(x => x.toLowerCase())
.join('_');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment