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
}
}
-
This smells so much like a homework assignment to me. Try searching, I'll start you off with the first solution: stackoverflow.com/questions/1027224/…Duniyadnd– Duniyadnd2016年02月19日 02:34:01 +00:00Commented Feb 19, 2016 at 2:34
-
I wouldn't use a loop when .replace() with a regular expression can do it.nnnnnn– nnnnnn2016年02月19日 02:36:19 +00:00Commented Feb 19, 2016 at 2:36
1 Answer 1
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"
1 Comment
return breed.split(/(?=[A-Z])/).join(' ').toLowerCase();.. see this example -> jsfiddle.net/L7m2x7uL