Files
30-seconds-of-code/snippets/stringToArrayOfWords.md
2017-12-20 12:34:03 +05:30

13 lines
404 B
Markdown

### stringToArrayOfWords
Converts a given string into an array of words
Use `String.split` to convert the string to an array.
```js
const stringToArrayOfWords = (str, pattern = /[\W_]+/) => str.split(pattern).filter(Boolean)
// stringToArrayOfWords("I love javaScript!!") -> ["I", "love", "javaScript"]
// stringToArrayOfWords("python, javaScript & coffee") -> ["python", "javaScript", "coffee"]
```