开发者

Javascript Regex - Replace { with nothing

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

sel开发者_StackOverflow中文版ector_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


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,'');
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜