Split a String into an Array Using the split Method

Split a String into an Array Using the split Method
0
#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

0 Likes

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

1 Like

#3

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

#5

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

try this instead.

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

0 Likes

#6

thanks your help ,just now I find a method split(/\s|\W/)

2 Likes

#7

Yup. That is way better. Welcome.

0 Likes

#8

Hi there, can anyone please tell me, why this did not work? : str.split(/\s+/)

0 Likes

#9

@SamuelJanoska Can you please use the Ask for Help link on the challenge you are working instead of asking your question here?

Thank you.

1 Like

#10

Even much better: str.split(/\W/)

0 Likes

#11

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?

0 Likes

#12

\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?

0 Likes

#13

Try to use it

str.split(/\s|\.|,|-/)
0 Likes