[英]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 一切正常,但是当我添加
selector_css = selector_css.replace(/{/g,'');
I dont get a result or an error, but why? 我没有得到结果或错误,但为什么? Can somebody help me? 有人能帮助我吗?
http://jsfiddle.net/beMKY/ 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. 但在这种情况下,问题是css_selector是一个对象(数组)而不是一个字符串。 You can apply replace
only to a string!!! 您只能将replace
应用于字符串! Use 采用
selector_css = selector_css[0].replace(/\{/g,'');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.