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.
<!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.
<!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:
Recommended Posts:
- How to convert string to camel case in JavaScript ?
- Convert string to title case in JavaScript
- Switch Case in JavaScript
- CSS | hyphens Property
- Case insensitive search in JavaScript
- Compare the Case Insensitive strings in JavaScript
- How to Convert Array to Set in JavaScript?
- How to convert a JavaScript date to UTC?
- How to convert Set to Array in JavaScript?
- How to convert decimal to hex in JavaScript ?
- Convert string into date using JavaScript
- How to convert a plain object into ES6 Map using JavaScript ?
- How to convert an object to string using JavaScript?
- How to convert a pixel value to a number value using JavaScript ?
- Convert an Array to an Object in JavaScript
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.



