I am wondering how to extract words (substrings) from a string, if said strings are between two specific characters. In my case, I am looking for the start character to be a white space and the final character to be a comma like so:
var str = "Hit that thing man! and a one, two, three, four, five, six, seven or eight";
Result:
var result = ["one", "two", "three", "four", "five", "six", "seven", "eight"];
I am wondering if a regex is possible, or perhaps good old javascript will be the straight forward solution.
I have tried the following so far:
var result = str.split(/[,\s]+/);
But to no avail since it does the following behavior incorrectly:
- Grabs the entire string before
one
. - Grabs the space before the desired letter.
Bonus round: Can I include the last letter eight
in the result by adding to the desired regex/javascript solution?
Any help is very appreciated!