views:

31

answers:

1

Hi,
i have a little problem and i have no idea what is wrong

    var selector_css;
    var sheet= document.styleSheets[0];
    var rules= 'cssRules' in sheet? sheet.cssRules : sheet.rules;
    for (var i= 0; i<rules.length; i++)
    {
        var rule= rules[i];
        var text= 'cssText' in rule? rule.cssText : rule.selectorText+' {'+rule.style.cssText+'}';
        text =  text.replace(/\s/g, '');
        selector_css = text.match(/^(.*?){/gi);
         // selector_css = selector_css.replace(/{/g,'');  // <- dont work ?
        $('body').append('- '+selector_css+' <br />');
    }

Everything works fine but when i add

selector_css = selector_css.replace(/{/g,''); 

I dont get a result or an error, but why? Can somebody help me?

http://jsfiddle.net/beMKY/

Thanks in advance!
Peter

+3  A: 

Some special chars like "." need to be escaped:

selector_css = selector_css.replace(/\{/g,''); 

But in this case the problem is that css_selector is a object (array) and not a string. You can apply replace only to a string!!! Use

selector_css = selector_css[0].replace(/\{/g,'');
Thariama
http://jsfiddle.net/beMKY/2/
Peter
// only to a string ... sorry, i was blind :) ... Thanks for the fast help!
Peter
glad it works for you now
Thariama
Nice, didn't catch that either. :)
Thomas