To Pascal Case

Converts a string to pascal casing.

Contributed by @itsbrunodev

javascript
function toPascalCase(str) {
  return str
    .replace(/^[^A-Za-z0-9]*|[^A-Za-z0-9]*$/g, "$")
    .replace(/[^A-Za-z0-9]+/g, "$")
    .replace(/([a-z])([A-Z])/g, (m, a, b) => `${a}$${b}`)
    .toLowerCase()
    .replace(/(\$)(\w?)/g, (m, a, b) => b.toUpperCase());
}
javascript
toPascalCase("hello world"); // "HelloWorld"

Keywords

string
casing