How to convert hyphens to camel case in JavaScript ?

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

Approach:

  • Store the string containg 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.



filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

  • Before clicking on the button:
    Image
  • After clicking on the button:
    Image

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

  • Before clicking on the button:
    Image
  • After clicking on the button:
    Image


My Personal Notes arrow_drop_up

Image
Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.