Reputation: 13
How do i make it so the function will take in the param (breed) and search the uppercase letter and add a space there.
for example if i pass "goldenRetriever" as the param, then the function will transform it into "golden retriever"
function test(breed){
for(i=1; i<breed.length; i++){
//wat do i do here
}
}
Upvotes: 0
Views: 95
Reputation: 241008
You could split the string before each uppercase letter using a regular expression with a positive lookahead, /(?=[A-Z])/
, then you could join the string back together with a space and convert it to lowercase:
"goldenRetrieverDog".split(/(?=[A-Z])/).join(' ').toLowerCase();
// "golden retriever dog"
Alternatively, you could also use the .replace()
method to add a space before each capital letter and then convert the string to lowercase:
"goldenRetrieverDog".replace(/([A-Z])/g, " $1").toLowerCase();
// "golden retriever dog"
Upvotes: 5