Reputation: 133
This is my input string:
var str = 'tags: JavaScript, Markdown, Web , programming skill ';
At first I filter the 'tags:' part by
var tags = str.match(/^tags:\s{0,}(.{1,})/);
Then the string is
'JavaScript, Markdown, Web , programming skill '
How could I get the result by RegExp-match.
['JavaScript', 'Markdown', 'Web', 'programming skill']
Thank everyone.
Upvotes: 0
Views: 83
Reputation: 174756
I suggest you to do string.replace
and then string.split
because your input contains more than one spaces at the middle.
> var str = 'tags: JavaScript, Markdown, Web , programming skill ';
> var res = str.replace(/tags:\s*|\s*$|( ) +/g, '$1');
> res.split(/\s*,\s*/)
[ 'JavaScript',
'Markdown',
'Web',
'programming skill' ]
tags:\s*|\s*$|( ) +
matches the leading tags
string along with the following spaces or the spaces which are at the end of the string or captures the single space at the middle and the spaces following that single space are matched. Replacing all the matched characters with the chars inside group index 1 would be resulted in removing trailing spaces, the string tags spaces
and the two or more spaces at the middle would be turned into one.
DEMO for the String.replace Part
Upvotes: 2
Reputation: 67968
tags:\s+|\s*,\s+
Split by this.Remove an empty element
later.See demo.
https://regex101.com/r/qB0jV1/4
Upvotes: 0