camelCase <-> selector-case in JavaScript w/o Regex :: ArtLung.com

Created 03/2002

camelCase <-> selector-case in JavaScript w/o Regex

Scott Andrew pointed to Tim Morgan's camelCase <-> selector-case functions. Screaming Jedi cool. But they use JavaScript regular expressions. JavaScript regular expressions I've never gotten cozy with. Perhaps I should seek counseling. Love the Regular Expressions in PHP and Cold Fusion though.

Anyway, here are some alternative functions, and a demonstration too. More on camelCase.

Useless.

Share and enjoy.

function noRegexToCamel(str) { // converts to camelCase
  str=str.split('-');
  var outstr = str[0];
  for(i=1;i<str.length;i++){
   outstr=outstr+str[i].charAt(0).toUpperCase()+str[i].substring(1)
  }
return outstr;
}

Try it! Enter something in selector-case below:


function noRegexToSel(str) { // converts to selector-case
  var outstr="";
  for(i=0;i<str.length;i++){
   (str.charAt(i)==str.charAt(i).toUpperCase())?outstr=outstr+'-'+str.charAt(i).toLowerCase():outstr=outstr+str.charAt(i);
  }
return outstr;
}

Try it! Enter something in camelCase below:

blog comments powered by Disqus