0

I need to check if the body has a certain class (may have several different classes), and if it does get it, and split it into an array.

eg:

 <body class="someclass s_windows_8">

So far I have just the class check working:

 var software = [];
 if($("body").is('body[class*=" s_"]')) {
      software["name"] = //windows out of s_windows_8
      software["version"] = //8 out of s_windows_8
 }

Hope that makes sense.

Thanks

Martin
  • 795
  • 1
  • 12
  • 31
  • Note that you shouldn't use an array if you're not using numeric indices. Use an object: `software = {}`. – nnnnnn Apr 18 '14 at 05:21
  • 1
    where is 1 out of s_windows_8 ? I don't see any 1 in your question – Amit Joki Apr 18 '14 at 05:26
  • Sorry, fixed! software["version"] = //8 out of s_windows_8 – Martin Apr 18 '14 at 05:28
  • Take a look at this [question about regular expression selectors and a wonderful plugin](http://stackoverflow.com/questions/190253/jquery-selector-regular-expressions/190255#190255) – WASasquatch Apr 18 '14 at 05:56

2 Answers2

0

You can do it pretty easily with a regular expression:

var software = {},
    allClasses = document.body.className,
    details = allClasses.match(/(?:^| )s_([^_]+)_(\d+)(?: |$)/);
if (details) {
  software["name"] = details[1];
  software["version"] = +details[2];
} else {
  // error, there was no class matching the pattern
}

If the version "number" needs to allow for non-numeric versions like "2b" then change the regex to something like: /(?:^| )s_([^_]+)_([^ ]+)(?: |$)/, and remove the unary plus operator from +details[2].

nnnnnn
  • 147,572
  • 30
  • 200
  • 241
  • Thanks, works a charm. not sure if the + is necessary though "= +details[2]" – Martin Apr 18 '14 at 05:34
  • The `+` converts the string version to an actual number. Obviously you can omit it if not needed (or if allowing for non-numeric version "numbers" as per the alternative regex I give at the end of my answer). – nnnnnn Apr 18 '14 at 05:35
  • Thanks for the explanation of the + Didn't realise, but makes sense. – Martin Apr 18 '14 at 05:47
0

well it sounds like you want to use an object if you have named properties for your "array", but the way you would want to do it is something like:

var software = {};
 if($("body").is('body[class*=" s_"]')) {
     var classes = $("body").get(0).className;
     classes = classes.split(' ');
     var classArr = [];
     for (var c = 0; c < classes.length; c++) {
          if (classes[c].contains('s_') {
             classArr = classes[c].split('_');
          }
     }
     software["name"] = classArr[1]; //windows out of s_windows_8
     software["version"] = classArr[2] //8 out of s_windows_8
 }

You could also do this with node.classList which is an array of the classes if you are using supported browsers only, but className just pulls up a string of all classes defined on a node

that will get you the class that starts with 's_' and then split it on the underscores and add the properties to the object.

tophallen
  • 1,033
  • 7
  • 12