Created
February 25, 2019 08:27
-
-
Save AndriiBozh/0d395475d67df2b12529641de1e7e08b to your computer and use it in GitHub Desktop.
CodeWars: Split CamelCase
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ASSIGNMENT | |
_______________________________ | |
Split a camelcase string into individual words, the return value must be a single string of words seporated by one whitespace. | |
The strings are to be split on the capital letters like so: | |
'StringStringString' => 'String String String' | |
_______________________________ | |
SOLUTION | |
_______________________________ | |
function splitter(str){ | |
return str.replace(/([a-z0-9])([A-Z])/g, '$1 $2'); | |
} | |
_______________________________ | |
_______________________________ | |
-----------extra--------------- | |
_______________________________ | |
if we want the string to be splitted on each capital letter | |
function splitter(str){ | |
return str.match(/^[A-Z]?[^A-Z]*|[A-Z][^A-Z]*/g) | |
.join(' ') | |
.replace(/\s+/g,' '); | |
} | |
//if we run splitter('string StringString), .join() method will give us one extra space between 'string' and 'String': string String String | |
// .replace() method will remove extra spaces |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment