xxf2ps
June 25, 2018, 7:42am
1
Tell us what’s happening:
Your code so far
function splitify(str) {
// Add your code below this line
return str.split( '');
// Add your code above this line
}
splitify("Hello World,I-am code");
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/functional-programming/split-a-string-into-an-array-using-the-split-method
Marmiz
June 25, 2018, 7:56am
2
The challenge asks that you split each string into words, but not only by space.
For instance
Hello-World // ["Hello", "World"]
For example you can use a regex that match any non a-z
character
Hope it helps.
1 Like
xxf2ps:
return str.split( ‘’);
if you want by space you must declare ’ ’ space not ‘’ nothing
look at this sites https://regexr.com/ select References and character classes
1 Like
xxf2ps:
return str.split(/\s|,|-/);why the last can’t pass
It is not passing because you forgot to include the period (.)
try this instead.
xxf2ps
June 28, 2018, 8:26am
6
thanks your help ,just now I find a method split(/\s|\W/)
2 Likes
Yup. That is way better. Welcome.
Hi there, can anyone please tell me, why this did not work? : str.split(/\s+/)
nzanoa
September 12, 2018, 2:25am
10
xxf2ps:
\s|\W
Even much better: str.split(/\W/)
Can someone explain what the \W regex means and why it works so perfectly here? It seems like it’s just reading for any character that’s not a word, but how does it know that “I-am” is not a word if it’s all one string?
\W matches a character which is not a-z, A-Z, 0-9, or the _ (underscore) character.
What is the exact regular expression and how is the regular expression being used with a specific string?