- Instant help with your JavaScript coding problems

Convert string to pascal case in JavaScript

Question:
How to convert string to pascal case in JavaScript?
Answer:
function toPascalCase(str){
    return (' ' + str).toLowerCase().replace(/[^a-zA-Z0-9]+(.)/g, (m, chr) => {
        return chr.toUpperCase()});
}

console.log(toPascalCase('the name field id     is name-field_id')); // TheNameFieldIdIsNameFieldId
Description:

The pascal case (PascalCase) means writing phrases without spaces or punctuation, indicating the separation of words with a single capitalized letter and the first letter is upper case. To convert a string to a pascal case in JavaScript you can use the replace method on the string and use regular expressions and callback functions to generate the right output.

Share "How to convert string to pascal case in JavaScript?"