From 05d52fe35a9d415949d862d3490cb04214c5e654 Mon Sep 17 00:00:00 2001 From: Arjun Mahishi Date: Wed, 20 Dec 2017 10:28:54 +0530 Subject: [PATCH] updated function --- snippets/stringToArrayOfWords.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/snippets/stringToArrayOfWords.md b/snippets/stringToArrayOfWords.md index c54e2cec4..d0cea5793 100644 --- a/snippets/stringToArrayOfWords.md +++ b/snippets/stringToArrayOfWords.md @@ -5,9 +5,9 @@ Converts a given string into an array of words First create an array of dirt that you want to remove from the string. Then replace each dirt with an empty string. Use `String.split(' ')` to conver the string to an array with the space as a delimiter. ```js -const stringToArrayOfWords = str =>{ - [".", ",", "?", "!", "& ", "(", ")", "[", "]"].map(d => str = str.replace(d, "")); - return str.split(" "); +const stringToArrayOfWords = str => { + [".", ",", "?", "!", "& ", "(", ")", "[", "]"].map(d => str = str.split(d).join("")); + return str.split(/[\W_]+/); } // stringToArrayOfWords("I love javaScript!!") -> ["I", "love", "javaScript"]