Skip to content
Related Articles

Related Articles

Improve Article

How to convert hyphens to camel case in JavaScript ?

  • Last Updated : 21 Apr, 2021

Given a string containing hyphens (-) and the task is to convert hyphens (-) into camel case of a string using JavaScript.
Approach: 
 

  • Store the string containing hyphens into variable.
  • Then use the RegExp to replace the hyphens and make the first letter of words as upperCase.

Example 1: This example converts the hyphens (‘-‘) into camel case by using RegExp and replace() method
 

html




<!DOCTYPE HTML>
<html>
    <head>
        <title>
            How to convert hyphens to
            camel case in JavaScript ?
        </title>
    </head>
     
    <body style = "text-align:center;">
         
        <h1 style = "color:green;" >
            GeeksForGeeks
        </h1>
         
        <p id = "GFG_UP" style =
            "font-size: 16px; font-weight: bold;">
        </p>
 
         
        <button onclick = "gfg_Run()">
            Click here
        </button>
         
        <p id = "GFG_DOWN" style =
            "color:green; font-size: 20px; font-weight: bold;">
        </p>
 
         
        <script>
            var el_up = document.getElementById("GFG_UP");
            var el_down = document.getElementById("GFG_DOWN");
             
            var str = "this-is-geeks-for-geeks";
             
            el_up.innerHTML = str;
             
            function gfg_Run() {
                el_down.innerHTML = str.replace(/-([a-z])/g,
                    function (g) { return g[1].toUpperCase(); });
            }        
        </script>
    </body>
</html>

Output: 
 

  • Before clicking on the button: 
     

  •  
  • After clicking on the button: 
     



  •  

Example 2: This example is quite similar to previous one and converts the hyphens (‘-‘) to camel case by using RegExp and replace() method.
 

html




<!DOCTYPE HTML>
<html>
    <head>
        <title>
            How to convert hyphens to
            camel case in JavaScript ?
        </title>
    </head>
     
    <body style = "text-align:center;">
         
        <h1 style = "color:green;" >
            GeeksForGeeks
        </h1>
         
        <p id = "GFG_UP" style =
            "font-size: 16px; font-weight: bold;">
        </p>
 
         
        <button onclick = "gfg_Run()">
            Click here
        </button>
         
        <p id = "GFG_DOWN" style =
            "color:green; font-size: 20px; font-weight: bold;">
        </p>
 
         
        <script>
            var el_up = document.getElementById("GFG_UP");
            var el_down = document.getElementById("GFG_DOWN");
             
            var str = "-a-computer-science-portal-for-geeks";
             
            el_up.innerHTML = str;
             
            function gfg_Run() {
                el_down.innerHTML = str.replace(/-([a-z])/g,
                    function (m, s) {
                        return s.toUpperCase();
                    });
            }        
        </script>
    </body>
</html>

Output: 
 

  • Before clicking on the button: 
     

  •  
  • After clicking on the button: 
     

  •  

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :