Reputation: 667
I'm trying to split out the whole words out of a string without whitespace or special characters.
So from
'(votes + downvotes) / views'
I'd like to create an array like the following
['votes', 'downvotes, 'views']
Have tried the following, but catching the parens and some whitespace. https://regex101.com/r/yX9iW8/1
Upvotes: 0
Views: 81
Reputation: 1027
It's very simple...
var matches = '(votes + downvotes) / views'.match(/[a-z]+/ig);
You must decide for your project what characters make a words, and min-length of word. It can be chars with digits and dash with min-length of 3 characters...
[a-z0-9-]{3,}
Good luck!
Upvotes: 0
Reputation: 386600
You could use /\w+/g
as regular expression in combination with String#match
var array = '(votes + downvotes) / views'.match(/\w+/g);
console.log(array);
Upvotes: 2
Reputation: 66334
You can use \W+
to split on all non-word characters
'(votes + downvotes) / views'.split(/\W+/g).filter(x => x !== '');
// ["votes", "downvotes", "views"]
Or \w+
to match on all word characters
'(votes + downvotes) / views'.match(/\w+/g);
// ["votes", "downvotes", "views"]
Upvotes: 1