[Vuejs]-How to convert camel case to proper case in Vue.js?

2👍

All you really need to do is

  1. replace every uppercase character with a space, followed by that character
  2. replace the first character with uppercase

This can be done like so:

const deCamelCase = str => str.replace(/[A-Z]/g, ' $&').replace(/^./, toUppercase);
const toUppercase = str => str.toUpperCase();

Breaking this down:

str => str.replace(/[A-Z]/g, ' $&')

This matches any uppercase character (latin alphabet only!) and replaces it with a string consisting of a space, plus the whole matched string, coded as $&. Note that we need to use the /g flag to make sure we match every instance.

str => str.replace(/^./, toUppercase)

This matches the first character in the string only, and replaces it using the toUppercase function, which I define on a separate line for readability.

Leave a comment