Split a String into an Array Using the split Method

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

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 :slight_smile:
Hope it helps.

1 Like

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

It is not passing because you forgot to include the period (.)

try this instead.

(/\s|,|.|-/)

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+/)

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?

Try to use it

str.split(/\s|\.|,|-/)
1 Like