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.
Here are two examples that split one string by spaces, then another by digits using a regular expression:
var str = "Hello World";Since strings are immutable, the
var bySpace = str.split(" ");
// Sets bySpace to ["Hello", "World"]
var otherString = "How9are7you2today";
var byDigits = otherString.split(/\d/);
// Sets byDigits to ["How", "are", "you", "today"]
split
method makes it easier to work with them.
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.
split
method.
testString: 'assert(code.match(/\.split/g), "Your code should use the split
method.");'
- text: 'splitify("Hello World,I-am code")
should return ["Hello", "World", "I", "am", "code"]
.'
testString: 'assert(JSON.stringify(splitify("Hello World,I-am code")) === JSON.stringify(["Hello", "World", "I", "am", "code"]), "splitify("Hello World,I-am code")
should return ["Hello", "World", "I", "am", "code"]
.");'
- text: 'splitify("Earth-is-our home")
should return ["Earth", "is", "our", "home"]
.'
testString: 'assert(JSON.stringify(splitify("Earth-is-our home")) === JSON.stringify(["Earth", "is", "our", "home"]), "splitify("Earth-is-our home")
should return ["Earth", "is", "our", "home"]
.");'
- text: 'splitify("This.is.a-sentence")
should return ["This", "is", "a", "sentence"]
.'
testString: 'assert(JSON.stringify(splitify("This.is.a-sentence")) === JSON.stringify(["This", "is", "a", "sentence"]), "splitify("This.is.a-sentence")
should return ["This", "is", "a", "sentence"]
.");'
```