8

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 comma

'lmn-button ,lmn-button-primary' => ['lmn-button', 'lmn-button-primary'] // Note the space before comma

'  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?

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 comma, one or more times"? Commented Apr 7, 2018 at 9:25
  • or also /([^,\s]+)/... Commented Apr 7, 2018 at 9:28
  • Just use var result = s.match(/[^,\s]+/g) Commented Apr 7, 2018 at 11:00

3 Answers 3

5

const arr = ' lmn-button ,lmn-button-primary'.trim().split(/\s*,\s*/);
console.log(arr);

Sign up to request clarification or add additional context in comments.

1 Comment

If there are spaces at start or end of the string ' lmn-button ,lmn-button-primary ', the regex is not working very well. I know I'm asking too much. Is it possible to handle that?
4

You may simply extract all substrings that consist of 1+ chars other than whitespace and comma.

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 commas and \s matches any whitespace chars, so [^,\s] matches any char but a comma 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));
}

Comments

1

You can you use this

console.log('lmn-button ,lmn-button-primary'.split(/[ ,.]+/));

1 Comment

This does not handle a string that starts or ends with spaces (or commas) -- in that case you would end up with empty strings in the array. I would use a trim() before the split() function. Also, using the \s whitespace char would handle embedded tabs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.