I need to split a CSS class names string into array of CSS class names in JavaScript. All the below strings should produce the same array.
'lmn-button,lmn-button-primary' => ['lmn-button', 'lmn-button-primary']
'lmn-button, lmn-button-primary' => ['lmn-button', 'lmn-button-primary'] // Note the space after ma
'lmn-button ,lmn-button-primary' => ['lmn-button', 'lmn-button-primary'] // Note the space before ma
' lmn-button ,lmn-button-primary' => ['lmn-button', 'lmn-button-primary'] // Note the space at start
'lmn-button ,lmn-button-primary ' => ['lmn-button', 'lmn-button-primary'] // Note the space at end
Currently I'm using code to do that,
cssClassesString.split(',').map(cssClass => cssClass.trim());
But I believe regex would be a better solution that this right?
I got this regex by googling /([^,]+)
but the result array has spaces in class names.
How can I improve the above regex to handle that?
I need to split a CSS class names string into array of CSS class names in JavaScript. All the below strings should produce the same array.
'lmn-button,lmn-button-primary' => ['lmn-button', 'lmn-button-primary']
'lmn-button, lmn-button-primary' => ['lmn-button', 'lmn-button-primary'] // Note the space after ma
'lmn-button ,lmn-button-primary' => ['lmn-button', 'lmn-button-primary'] // Note the space before ma
' lmn-button ,lmn-button-primary' => ['lmn-button', 'lmn-button-primary'] // Note the space at start
'lmn-button ,lmn-button-primary ' => ['lmn-button', 'lmn-button-primary'] // Note the space at end
Currently I'm using code to do that,
cssClassesString.split(',').map(cssClass => cssClass.trim());
But I believe regex would be a better solution that this right?
I got this regex by googling /([^,]+)
but the result array has spaces in class names.
How can I improve the above regex to handle that?
Share Improve this question edited Jun 29, 2018 at 4:16 VJAI asked Apr 7, 2018 at 9:23 VJAIVJAI 32.8k23 gold badges106 silver badges166 bronze badges 3-
I would suggest to avoid regex where string methods can do the job, but if you prefer regex, what about:
/([^, ]+)/
which means "any character but not space or ma, one or more times"? – urban Commented Apr 7, 2018 at 9:25 -
or also
/([^,\s]+)/
... – Matteo Ragni Commented Apr 7, 2018 at 9:28 -
Just use
var result = s.match(/[^,\s]+/g)
– Wiktor Stribiżew Commented Apr 7, 2018 at 11:00
3 Answers
Reset to default 5
const arr = ' lmn-button ,lmn-button-primary'.trim().split(/\s*,\s*/);
console.log(arr);
You may simply extract all substrings that consist of 1+ chars other than whitespace and ma.
var result = s.match(/[^,\s]+/g)
See the regex demo.
The [^...]
is a negated character class matching any char but the one(s) specified in the class. ,
matches mas and \s
matches any whitespace chars, so [^,\s]
matches any char but a ma and whitespace. +
quantifier matches 1+ consecutive occurrences of such chars.
JS demo:
var tests = ['lmn-button,lmn-button-primary', 'lmn-button, lmn-button-primary', 'lmn-button ,lmn-button-primary',' lmn-button ,lmn-button-primary', 'lmn-button ,lmn-button-primary '];
var rx = /[^\s,]+/g;
for (var s of tests) {
console.log(s, "=>", s.match(rx));
}
You can you use this
console.log('lmn-button ,lmn-button-primary'.split(/[ ,.]+/));