From 1fc0e8754044d6db6b5071ddaa5ff325f90e12fe Mon Sep 17 00:00:00 2001 From: Arjun Mahishi Date: Wed, 20 Dec 2017 13:06:12 +0530 Subject: [PATCH] Updated function for not splitting hyphenated words --- snippets/stringToArrayOfWords.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/stringToArrayOfWords.md b/snippets/stringToArrayOfWords.md index 01bd6eb8b..885216a58 100644 --- a/snippets/stringToArrayOfWords.md +++ b/snippets/stringToArrayOfWords.md @@ -5,7 +5,7 @@ Converts a given string into an array of words Use String.split with a supplied pattern(defaults to non alpha as a regex) to convert to an Array of Strings and then filter to remove any empty strings. ```js -const stringToArrayOfWords = (str, pattern = /[\W_]+/) => str.split(pattern).filter(Boolean) +const stringToArrayOfWords = (str, pattern = /[^a-zA-Z-]+/) => str.split(pattern).filter(Boolean) // stringToArrayOfWords("I love javaScript!!") -> ["I", "love", "javaScript"] // stringToArrayOfWords("python, javaScript & coffee") -> ["python", "javaScript", "coffee"]