freeCodeCamp Challenge Guide: Split a String into an Array Using the split Method

Split a String into an Array Using the split Method


Problem Explanation

Simply split the string to create a new array of words.

A simple regular expression can be used to achieve this result.

/\W/ Matches any non-word character. This includes spaces and punctuation, but not underscores. It’s equivalent to /[^A-Za-z0-9_]/. For more information about Regular Expressions, see the official MDN Documentation.


Solutions

Solution 1 (Click to Show/Hide)
function splitify(str) {
  // Add your code below this line
  return str.split(/\W/);
  // Add your code above this line
}
splitify("Hello World,I-am code");
57 Likes

Hey campers,here is how I approached the challenge:

var string = “Split me into an array”;
var array = [];

// Only change code below this line.

array = string.split(/\s+/g);

23 Likes