How to convert hyphens to camelCase in JavaScript?

王林
Release: 2023-09-05 10:21:06
forward
702 people have browsed it

如何在 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
Copy after login

Here are a few ways to convert hyphens to camelCase using JavaScript.

  • Use String.replace() method

  • Use Array.map() and Array.join() methods

Use String.replace() method

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.

Example

In this example, we use the String.replace method to convert a hyphenated string to camelCase format.

   Converting Hyphenated string into Camel case string in JavaScript 
  

Converting Hyphenated string into Camel case string using String.replace() method

String with hyphens:

String after converting hyphens to camel case:

Copy after login

Use Array.map() and Array.join() methods

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.

Example

In this example, we convert the hyphenated string to camelCase string.

  Converting Hyphenated string into Camel case string in JavaScript 
  

Convert Hyphenated string into Camel case string using Array.map() and Array.join() methods

String with hyphens:

String after converting hyphens to camel case:

Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!