Home >Web Front-end >JS Tutorial >How to convert hyphens to camelCase in JavaScript?
As developers, we often encounter hyphenated strings. Hyphenated strings make our code more readable when string lengths are long and names are very complex. To solve this problem we use camel case. CamelCase is a very popular naming convention in which we combine multiple words and form a string by capitalizing the first letter of each string except the first. In javascript, we can use this convention to create variable and function names, but we cannot use hyphenated strings to create variables. In this article, we'll step through the various methods of converting hyphens to camelCase. By the end of this article, you will be able to apply techniques to improve the readability and maintainability of your code.
Here are some examples of converting hyphens to camelCase -
Input: this-is-an-object Output: thisIsAnObject Input: convert-hyphens-to-camel-case Output: convertHyphensToCamelCase
Here are a few ways to convert hyphens to camelCase using JavaScript.
Use String.replace() method
Use Array.map() and Array.join() methods
String.replace method is a built-in method in javascript, used to replace the specified value with another value in the string. The following is a step-by-step procedure for converting a hyphenated string to a camelCase string using the String.replace method.
Use the first parameter of the String.replace method to search for all letters following a hyphen.
You can use regular expressions such as /-([a-z])/g
This regular expression selects two elements, the first is a hyphen and the second is the letter after the hyphen.
In the second parameter of the String.replace method, return the uppercase value of the second letter.
In this example, we use the String.replace method to convert a hyphenated string to camelCase format.
<!DOCTYPE html> <html lang="en"> <head> <title>Converting Hyphenated string into Camel case string in JavaScript</title> </head> <body> <h3>Converting Hyphenated string into Camel case string using String.replace() method</h3> <p id="input">String with hyphens: </p> <p id="output"> String after converting hyphens to camel case: </p> <script> // The input String let inp = "this-is-an-object"; document.getElementById("input").innerHTML += inp; // Search for the letter after the hyphen and return the uppercase value inp = inp.replace(/-([a-z])/g, function(k){ return k[1].toUpperCase(); }) // Print the camel case value document.getElementById("output").innerText += inp; </script> </body> </html>
The Array.map() method is JavaScript that creates a new array after applying a function in each element of the array. This method does not modify the original array.
Array.join method is used to convert an array into a string by joining all elements of the array.
To convert a hyphenated string to a camel case string, we use the following steps.
Split array elements from each hyphen using the String.split method. Now we have an array which contains all the words of String as array elements.
Now use the Array.map and String.toUpperCase methods to convert the first letter of each element to uppercase.
Now use the Array.join method to join the Arary elements. Finally, we get the camel case string.
In this example, we convert the hyphenated string to camelCase string.
<html> <head> <title>Converting Hyphenated string into Camel case string in JavaScript</title> </head> <body> <h3>Convert Hyphenated string into Camel case string using Array.map() and Array.join() methods</h3> <p id="input">String with hyphens: </p> <p id="output"> String after converting hyphens to camel case: </p> <script> // The input String let inp = "this-is-an-object"; document.getElementById("input").innerHTML += inp; // Convert the String into an Array inp = inp.split("-"); // Remove and save the first element let firstString = inp.shift(); // Convert every first letter into uppercase inp = inp.map(function(k){ return k[0].toUpperCase() + k.substring(1); }); // Join the Array inp = inp.join(""); // Concatenate the first element inp = firstString + inp; // Print the camel case value document.getElementById("output").innerText += inp; </script> </body> </html>
The above is the detailed content of How to convert hyphens to camelCase in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!