最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Javascript Regex - Replace { with nothing - Stack Overflow

programmeradmin7浏览0评论


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?

/

Thanks in advance!
Peter


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/beMKY/

Thanks in advance!
Peter

Share Improve this question edited Sep 30, 2010 at 7:50 Thariama 50.8k13 gold badges145 silver badges175 bronze badges asked Sep 30, 2010 at 7:34 PeterPeter 11.8k31 gold badges101 silver badges155 bronze badges 1
  • You have to escape the braces like so: /\{/g – Thomas Commented Sep 30, 2010 at 7:35
Add a ment  | 

1 Answer 1

Reset to default 5

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)

  1. 暂无评论