JavaScript: Make a string uncamelize
JavaScript String: Exercise-12 with Solution
Write a JavaScript function to uncamelize a string.
Test Data:
console.log(uncamelize('helloWorld'));
console.log(uncamelize('helloWorld','-'));
console.log(uncamelize('helloWorld','_'));
"hello world"
"hello-world"
"hello_world"
Pictorial Presentation:

Sample Solution:-
HTML Code:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JavaScript uncamelize function</title>
</head>
<body>
</body>
</html>
JavaScript Code:
function uncamelize(str, separator) {
// Assume default separator is a single space.
if(typeof(separator) == "undefined") {
separator = " ";
}
// Replace all capital letters by separator followed by lowercase one
var str = str.replace(/[A-Z]/g, function (letter)
{
return separator + letter.toLowerCase();
});
// Remove first separator
return str.replace("/^" + separator + "/", '');
}
console.log(uncamelize('helloWorld'));
console.log(uncamelize('helloWorld','-'));
console.log(uncamelize('helloWorld','_'));
Sample Output:
hello world hello-world hello_world
Flowchart:

Live Demo:
See the Pen JavaScript Make a string uncamelize - string-ex-12 by w3resource (@w3resource) on CodePen.
Improve this sample solution and post your code through Disqus
Previous: Write a JavaScript function to convert a string into camel case.
Next: Write a JavaScript function to concatenates a given string n times (default is 1).
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
JavaScript: Tips of the Day
How to check whether a string contains a substring in JavaScript?
ECMAScript 6 introduced String.prototype.includes:
const string = "foo"; const substring = "oo"; console.log(string.includes(substring));
includes doesn't have Internet Explorer support, though. In ECMAScript 5 or older environments, use String.prototype.indexOf, which returns -1 when a substring cannot be found:
var string = "foo"; var substring = "oo"; console.log(string.indexOf(substring) !== -1);
Ref: https://bit.ly/3fFFgZv
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework