Create a regular expression for all white spaces and underscores.
Hint 2
You will also have to make everything lowercase.
Hint 3
The tricky part is getting the regular expression part to work, once you do that then just turn the uppercase to lowercase and replace spaces with dashes using replace().
Solutions
Solution 1 (Click to Show/Hide)
function spinalCase(str) {
// Create a variable for the white space and underscores.
var regex = /\s+|_+/g;
// Replace low-upper case to low-space-uppercase
str = str.replace(/([a-z])([A-Z])/g, "$1 $2");
// Replace space and underscore with -
return str.replace(regex, "-").toLowerCase();
}
// test here
spinalCase("This Is Spinal Tap");
Code Explanation
regex contains the regular expression /\s+|_+/g, which will select all white spaces and underscores.
The first replace() puts a space before any encountered uppercase characters in the string str so that the spaces can be replaced by dashes later on.
While returning the string, another replace() replaces spaces and underscores with dashes using regex.
Solution 2 (Click to Show/Hide)
function spinalCase(str) {
// Replace low-upper case to low-space-uppercase
str = str.replace(/([a-z])([A-Z])/g, "$1 $2");
// Split on whitespace and underscores and join with dash
return str
.toLowerCase()
.split(/(?:_| )+/)
.join("-");
}
// test here
spinalCase("This Is Spinal Tap");
Code Explanation
Similar to the first solution, the first replace() puts a space before any encountered uppercase characters in the string str so that the spaces can be replaced by dashes later on.
Instead of using replace() here to replace whitespace and underscores with dashes, the string is split() on the regular expression /(?:_| )+/ and join()-ed on -.
function spinalCase(str) {
// "It's such a fine line between stupid, and clever."
// --David St. Hubbins
return str
.split(/\s|_|(?=[A-Z])/)
.join("-")
.toLowerCase();
}
Code Explanation
Split the string at one of the following conditions (converted to an array)
So this is what I came up with myself (I am not great with regular expressions)
function spinalCase(str) {
// "It's such a fine line between stupid, and clever."
// --David St. Hubbins
str = str.replace(/[^a-zA-Z0-9-]|[a-z][A-Z]/g, function(x) {
if (x.match(/[a-z][A-Z]/)) {
return x[0] + "-" + x[1];
} else {
return "-";
}
});
return str.replace(/[A-Z]/g, function(x){
return x.toLowerCase();
});
}
@ArielLeslie@jemagee I am also not good with regular expressions, so I came up with this.
function spinalCase(str) {
var result="";
for (var i = 0; i<str.length; i++){
var codeCheck = str.charCodeAt(i);
if(codeCheck>=65 && codeCheck<=90){
result+=String.fromCharCode(codeCheck+32); // converting to lower case
}
else if(codeCheck>=97 && codeCheck<=122){
if(str.charCodeAt(i+1)>=65 && str.charCodeAt(i+1)<=90){ // if the next character is Uppercase, then add the "-"
result+=String.fromCharCode(codeCheck);
result+="-";
}
else
result+=String.fromCharCode(codeCheck);
}
else
result+="-";
}
return result;
}
spinalCase('thisIsSpinalTap aBCDefK');
It seems to be working fine.
If you detect any flaws in this code, kindly reply. I need to get better at regular expressions. For now, just trying to avoid the use of it.
Could you help me understand your solution a little bit more? I like the conciseness of it, but I don’t understand the last part.
So far, this is my understanding of that code:
Return the result of the replace method called on the string argument (thereby creating a new “str”),
For every lower case a to z letter (RegExp), every capital A-Z letter (RegExp) -OR- underscore or blank space characters of at least one or more, global search…
…and then the syntax for the $1 and $2 I don’t understand.
The MDN documentation says that $n takes any positive integer below 100 and assigns it to the nth “parenthesized submatch string”, assuming that the first argument fed to .replace is a RegExp object.
So, with the above syntax, I am guessing it would read: for every capital or lower case a-z OR a blank space/underscore, replace it with… that same letter or blank space? I’m really not sure.
Sure thing! You’re right about the first part. So the regex stores the match found in each bracket as a ‘$’ variable. It looks for a small letter next to a capital letter. Once it has found that it assigns the first letter to the ‘$1’ variable and the second letter to the ‘$2’ variable. It then replaces that match with the string on the right which contains a dash in the middle between the two characters and calls toLowerCase on the result.
I found the same thing testing it with this string: ‘AllThe-Small ThingsFor’. The combination of the hyphen and uppercase letter trips up the advanced solution but not the others. However, with your string none of the solutions work. I’m assuming you want the result to be ‘this-is-spinal-case-ii’, right? I guess this is an edge case as you would have to tell the algorithm that ‘II’ is not a word that you can split up but a roman number.
To make the solution work for my example string I added a replace to it:
function spinalCase(str) {
return str.split(/\s|_|(?=[A-Z])/).join('-').replace(/--/g, '-').toLowerCase();
}
It is not very elegant so any suggestions for improvement would be appreciated.
adding non capturing group like: return str.split(/\s|_|(?=[A-Z])|(?:-)/).join('-').toLowerCase()
will get rid of - all together, since when joining we add them anyway, those fixing the issue.
As for “iI” being separated, well the idea is to catch entire words beginning with capital letter. Identifying what is basically a typing error is slightly beyond scope of this exercise I recon. Just imagine writing regex to extract the word from random mix of low and upper caps like FrEEcoDeCaMp
I’m not good enough with regex,but this is my approach.
function spinalCase(str) {
// "It's such a fine line between stupid, and clever."
// --David St. Hubbins
var firstLet = (str.split(''))[0];
str = str.slice(1,str.length);
str = str.replace(/\s/g,'-');
str = str.replace(/([A-Z])/g,'-$1');
str = str.replace(/_/g,'-');
str = str.replace(/-+/g,'-');
return (firstLet + str).toLowerCase();
}
spinalCase('thisIsSpinalTap');
I got some trouble with this challenge, but finally I managed to solve it. My major problem was the “upper case letter condition” to insert a “-”. I know there are short and easy ways to solve this using regExp but it’s a bit complicated for me since I don’t have so much practice in that (by now), so I wanted to find my way through this challenge.
function spinalCase(str) {
// replace spaces [/s] and underscore [_]
return str.replace(/[\s_]/g, "-")
// replace lowercase letter [a-z] followed by uppercase letter [A-Z]
.replace(/[a-z](?=[A-Z])/g, function(match) {
match = match + "-";
return match;
})
.toLowerCase();
}
function spinalCase(str) {
// "It's such a fine line between stupid, and clever."
// --David St. Hubbins
var arr = str.split("");
var regEx = /[A-Z]/g;
for(var i =0; i<arr.length;i++){
if(regEx.test(arr[i])){
if (i!=0){
arr.splice(i, 0, " ");
}
}
}
var filtered = arr.join("").toLowerCase().replace(/_/g," ").replace(/\s+/g," ").replace(/[^A-Za-z0-9]/g,"-");
return filtered;
//finito
}
I really need to get better with regular expressions. Some algorithms here have just three to four lines of code!! I’ll get there soon though. I’ll get there.