首页 > 文章列表 > 如何在 JavaScript 中将连字符转换为驼峰式大小写?

如何在 JavaScript 中将连字符转换为驼峰式大小写?

328 2023-09-18

作为开发人员,我们经常遇到带连字符的字符串。当字符串长度较长且名称非常复杂时,连字符字符串使我们的代码更具可读性。为了解决这个问题,我们使用了驼峰箱。驼峰命名法是一种非常流行的命名约定,其中我们将多个单词组合起来,并通过将除第一个字符串之外的每个字符串的第一个字母大写来构成一个字符串。在 javascript 中,我们可以使用此约定来创建变量和函数名称,但不能使用连字符字符串来创建变量。在本文中,我们将逐步探索将连字符转换为驼峰式大小写的多种方法。在本文结束时,您将能够应用技术来提高代码的可读性和可维护性。

以下是一些将连字符转换为驼峰式大小写的示例 -

Input: this-is-an-object 
Output: thisIsAnObject
Input: convert-hyphens-to-camel-case
Output: convertHyphensToCamelCase

以下是使用 JavaScript 将连字符转换为驼峰式大小写的几种方法。

  • 使用 String.replace() 方法

  • 使用 Array.map() 和 Array.join() 方法

使用 String.replace() 方法

String.replace方法是javascript中的内置方法,用于将指定值替换为字符串中的另一个值。以下是使用 String.replace 方法将连字符字符串转换为驼峰式大小写字符串的分步过程。

  • 使用 String.replace 方法的第一个参数搜索连字符后面的所有字母。

  • 您可以使用正则表达式,例如/-([a-z])/g

  • 此正则表达式选择两个元素,第一个是连字符,第二个是连字符之后的字母。

  • 在 String.replace 方法的第二个参数中,返回第二个字母的大写值。

示例

在此示例中,我们使用 String.replace 方法将连字符字符串转换为驼峰式大小写格式。

<!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>

使用 Array.map() 和 Array.join() 方法

Array.map() 方法是 JavaScript,它在数组的每个元素中应用函数后创建一个新数组。此方法不会修改原始数组。

Array.join 方法用于通过连接数组的所有元素将数组转换为字符串。

要将连字符字符串转换为驼峰式大小写字符串,我们使用以下步骤。

  • 使用 String.split 方法从每个连字符中拆分数组元素。现在我们有一个数组,其中包含 String 的所有单词作为数组元素。

  • 现在使用 Array.map 和 String.toUpperCase 方法将每个元素的第一个字母转换为大写。

  • 现在使用 Array.join 方法连接 Arary 元素,最后,我们得到了驼峰式字符串。

示例

在此示例中,我们将连字符字符串转换为驼峰式大小写字符串。

<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>