How to use Regex and Callback Function In Javascript

The Regex and Callback Function approach uses a regular expression to match specific patterns in a string, with a callback function to handle the replacement. In this case, it replaces hyphens or underscores followed by a character with that character in uppercase, effectively converting the string to camel case.

Example: In this example The toCamelCase function converts hyphen or underscore-separated strings to camelCase by replacing these separators with uppercase letters after them, as shown in the console.log statements.

JavaScript
function toCamelCase(str) {
  return str.replace(/[-_](.)/g, (match, char) => char.toUpperCase());
}

console.log(toCamelCase('hello_world')); 
console.log(toCamelCase('foo-bar-baz')); 

Output
helloWorld
fooBarBaz




How to convert string to camel case in JavaScript ?

We will be given a string and we have to convert it into the camel case. In this case, the first character of the string is converted into lowercase, and other characters after space will be converted into uppercase characters. These camel case strings are used in creating a variable that has meaning.

Example of converting string to camel case in JavaScript

Table of Content

  • Using the str.replace() method
  • Using reduce() and split() method
  • Using the Lodash _.camelCase() Method
  • Using Array.map() and Array.join()
  • Using a combination of String method
  • Using Regex and Callback Function

Similar Reads

Using the str.replace() method

Use the str.replace() method to replace the first character of the string in lower case and other characters after space will be in upper case. The toUpperCase() and toLowerCase() methods are used to convert the string character into upper case and lower case respectively....

Using reduce() and split() method

Use reduce() method to iterate over the character of the string and convert it into camel case. The toUpperCase() and toLowerCase() methods are used to convert the string character into upper case and lower case respectively....

Using the Lodash _.camelCase() Method

In this approach, we will use the lodash _.camelCase() method which will conver the given string into the camel case....

Using Array.map() and Array.join()

In this approach first we split the string at hyphens (-) . Then using map() method we will iterate over each word in the array of substrings. For the first word (at index 0), no changes are made, For subsequent words, we will capitalized the first character and then concatenate with the rest of the word....

Using a combination of String method

The combination approach converts a string to camelCase by first making it lowercase. Then, it uses a regular expression to find all instances of a dash or underscore followed by a lowercase letter, and replaces them with the uppercase version of the letter....

Using Regex and Callback Function

The Regex and Callback Function approach uses a regular expression to match specific patterns in a string, with a callback function to handle the replacement. In this case, it replaces hyphens or underscores followed by a character with that character in uppercase, effectively converting the string to camel case....