Pascalcase string

JavaScript, String, Regexp · Sep 8, 2021

Converts a string to pascal case.

const toPascalCase = 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.charAt(0).toUpperCase() + x.slice(1).toLowerCase())
    .join('');
toPascalCase('some_database_field_name'); // 'SomeDatabaseFieldName'
toPascalCase('Some label that needs to be pascalized');
// 'SomeLabelThatNeedsToBePascalized'
toPascalCase('some-javascript-property'); // 'SomeJavascriptProperty'
toPascalCase('some-mixed_string with spaces_underscores-and-hyphens');
// 'SomeMixedStringWithSpacesUnderscoresAndHyphens'

More like this

  • JavaScript String Casing

    Convert between the most common string casing formats with pure JavaScript and a sprinkle of regular expressions.

    Collection · 6 snippets

  • Titlecase string

    Converts a string to title case.

    JavaScript, String · Oct 22, 2020

  • Camelcase string

    Converts a string to camelcase.

    JavaScript, String · Oct 22, 2020

  • Snakecase string

    Converts a string to snake case.

    JavaScript, String · Jun 28, 2021