- Instant help with your JavaScript coding problems

Convert string to camel case in JavaScript

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

console.log(toCamelCase('The name field id is name-field_id')); //theNameFieldIdIsNameFieldId
Description:

The camel case (camelCase) means writing phrases without spaces or punctuation, indicating the separation of words with a single capitalized letter and the first letter is lower case. To convert a string to a camel 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 camel case in JavaScript?"