0

Is there a fast, reliable way to convert a space-separated class list like:

classOne classTwo classThree

to a selector like:

$('.classOne .classTwo .classThree')

I'm thinking it could done with either a loop that builds a selector string, or a series of regular expressions (one to clean up extra spaces and a second to convert [space] to [space].), but I'd like to know if there’s a built-in way, or something more concise/efficient/clever.

Nick
  • 10,904
  • 10
  • 49
  • 78

5 Answers5

5

Sure

var klass = "classOne classTwo classThree";
var arr   = klass.split(/\s+/);
var selector   = '.' + arr.join('.');

$(selector)

FIDDLE

or more directly

$('.' + klass.split(/\s+/).join('.') )
shreyas d
  • 774
  • 1
  • 4
  • 16
adeneo
  • 312,895
  • 29
  • 395
  • 388
3

Simple, use .replace :

var className = 'classOne classTwo classThree';
$(className.replace(/(^ *| +)/g, '$1.'));
Karl-André Gagnon
  • 33,662
  • 5
  • 50
  • 75
  • Could you explain what the carrot and the pipe do in that context? – Nick Dec 15 '14 at 19:44
  • 1
    Let start with the pipe. The pipe is just a simple `or` so it will match either the left side or the right side of the pipe. `^` mean *start of the string*. so basically, `(^| )` mean *match the start of the string OR a space and replace it with what you just matched + a dot.* – Karl-André Gagnon Dec 15 '14 at 19:47
2

How this compact RegEx:

var className = 'classOne classTwo classThree';
$(className.replace(/\b(\w)/g, '.$1'));

This will replace every set of word characters (ex: some classes) in the class list with dot + the word (ex: .some.classes) and then stick the result directly to a jQuery selector.

Robbie Wxyz
  • 7,671
  • 2
  • 32
  • 47
Toto
  • 89,455
  • 62
  • 89
  • 125
  • @M42: Maybe you could even get [this hat](http://winterbash2014.stackexchange.com/business-in-the-front-party-in-the-back) from it. – Robbie Wxyz Dec 16 '14 at 19:21
0

If you don't like regular expressions:

var className = "classOne classTwo classThree";
var selector = className.split(" ").map(function (_class) {return "." + _class}).join(" ");

You can split className with whitespaces and then use Array.prototype.map to create new array ([".classOne", ".classTwo", ".classThree"]). And then you just join it with whitespaces again.

  • 1
    This answer turned up in the low quality review queue, presumably because you didn't explain the code. If you do explain it (in your answer), you are far more likely to get more upvotes—and the questioner is more likely to learn something! – The Guy with The Hat Dec 15 '14 at 20:26
0

Based on this other answer,

var selector = className
    .replace(/(?=[^ \w])/g, '\\')   // Escape non-word characters
    .replace(/\b\d/g, '\\00003$&')  // Escape digits at the beginning
    .replace(/(^| +)(?!$| )/g, '.') // Add "." before classes, removing spaces

It works with

  • Untrimmed strings (at the beginning).
  • "Strange" class characters (/, $, *, etc.)
  • Classes which begin with a digit (invalid identifiers)
Community
  • 1
  • 1
Oriol
  • 274,082
  • 63
  • 437
  • 513