2018-10-10 18:03:03 -04:00
---
id: 587d7daa367417b2b2512b6b
2021-02-06 04:42:36 +00:00
title: Split a String into an Array Using the split Method
2018-10-10 18:03:03 -04:00
challengeType: 1
2020-08-05 16:38:04 +08:00
forumTopicId: 18305
2021-01-13 03:31:00 +01:00
dashedName: split-a-string-into-an-array-using-the-split-method
2018-10-10 18:03:03 -04:00
---
2020-12-16 00:37:30 -07:00
# --description--
2021-02-06 04:42:36 +00:00
The `split` method splits a string into an array of strings. It takes an argument for the delimiter, which can be a character to use to break up the string or a regular expression. For example, if the delimiter is a space, you get an array of words, and if the delimiter is an empty string, you get an array of each character in the string.
2020-12-16 00:37:30 -07:00
2021-02-06 04:42:36 +00:00
Here are two examples that split one string by spaces, then another by digits using a regular expression:
2020-08-05 16:38:04 +08:00
```js
var str = "Hello World";
var bySpace = str.split(" ");
// Sets bySpace to ["Hello", "World"]
var otherString = "How9are7you2today";
var byDigits = otherString.split(/\d/);
// Sets byDigits to ["How", "are", "you", "today"]
```
2021-02-06 04:42:36 +00:00
Since strings are immutable, the `split` method makes it easier to work with them.
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
# --instructions--
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
Use the `split` method inside the `splitify` function to split `str` into an array of words. The function should return the array. Note that the words are not always separated by spaces, and the array should not contain punctuation.
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
# --hints--
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
Your code should use the `split` method.
2018-10-10 18:03:03 -04:00
```js
2020-12-16 00:37:30 -07:00
assert(code.match(/\.split/g));
2018-10-10 18:03:03 -04:00
```
2021-02-06 04:42:36 +00:00
`splitify("Hello World,I-am code")` should return `["Hello", "World", "I", "am", "code"]` .
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert(
JSON.stringify(splitify('Hello World,I-am code')) ===
JSON.stringify(['Hello', 'World', 'I', 'am', 'code'])
);
```
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
`splitify("Earth-is-our home")` should return `["Earth", "is", "our", "home"]` .
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert(
JSON.stringify(splitify('Earth-is-our home')) ===
JSON.stringify(['Earth', 'is', 'our', 'home'])
);
```
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
`splitify("This.is.a-sentence")` should return `["This", "is", "a", "sentence"]` .
2018-10-10 18:03:03 -04:00
```js
2020-12-16 00:37:30 -07:00
assert(
JSON.stringify(splitify('This.is.a-sentence')) ===
JSON.stringify(['This', 'is', 'a', 'sentence'])
);
2018-10-10 18:03:03 -04:00
```
2020-08-05 16:38:04 +08:00
2021-01-13 03:31:00 +01:00
# --seed--
## --seed-contents--
```js
function splitify(str) {
// Only change code below this line
// Only change code above this line
}
splitify("Hello World,I-am code");
```
2020-12-16 00:37:30 -07:00
# --solutions--
2021-01-13 03:31:00 +01:00
```js
function splitify(str) {
// Only change code below this line
return str.split(/\W/);
// Only change code above this line
}
```