[英]Node.js regex: Find all CSS properties in a stylesheet not in the comments
I am trying to use regex to query all of the attributes inside of a CSS file. 我正在尝试使用正则表达式来查询CSS文件中的所有属性。 My end goal is to be able to take a css file like this: 我的最终目标是能够采用这样的css文件:
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed,
figure, figcaption, footer, header, hgroup,
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
margin: 0;
padding: 0;
border: 0;
font-size: 100%;
font: inherit;
vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure,
footer, header, hgroup, menu, nav, section {
display: block;
}
body {
line-height: 1;
}
ol, ul {
list-style: none;
}
blockquote, q {
quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
content: '';
content: none;
}
table {
border-collapse: collapse;
border-spacing: 0;
}
and return a list of selectors like this: 并返回一个这样的选择器列表:
margin, padding, border, font-size, font, vertical-align, display, list-style, quotes, content, border-collapse, border-spacing
The first step is to create a regular expression than only matches all of the attributes in the css file, and only those that are not selectors (ie. a:hover) or inside of comments. 第一步是创建一个正则表达式,而不仅仅匹配css文件中的所有属性,而只创建那些不是选择器(即a:hover)或注释内部的属性。
Right now all I have is /(?:([\\w\\d\\S\\-\\_]+)\\:)/g
, which works, but still queries selectors and text inside of comments. 现在我只有/(?:([\\w\\d\\S\\-\\_]+)\\:)/g
,它可以工作,但仍然可以在注释中查询选择器和文本。
Try using String.prototype.replace()
with RegExp
/\\/\\*.*\\*\\/|(-moz-|-ms-|-o-|-webkit-)+\\w+(?=:)/g
to match , remove comment text , vendor prefixes ; 尝试使用String.prototype.replace()
和RegExp
/\\/\\*.*\\*\\/|(-moz-|-ms-|-o-|-webkit-)+\\w+(?=:)/g
String.prototype.replace()
/\\/\\*.*\\*\\/|(-moz-|-ms-|-o-|-webkit-)+\\w+(?=:)/g
String.prototype.replace()
/\\/\\*.*\\*\\/|(-moz-|-ms-|-o-|-webkit-)+\\w+(?=:)/g
String.prototype.replace()
/\\/\\*.*\\*\\/|(-moz-|-ms-|-o-|-webkit-)+\\w+(?=:)/g
匹配,删除评论文本,供应商前缀; String.prototype.match()
with RegExp
/[az-]+(?=:[^before|after|hover])/ig
to match characters "a" through "z" or "-" case insensitive, followed by ":" , not followed by "before" or "after" String.prototype.match()
与RegExp
/[az-]+(?=:[^before|after|hover])/ig
匹配字符“a”到“z”或“ - ”不区分大小写,后跟“ :“,之前没有”之后“或之后”
var style = document.querySelector("style"); var props = style.textContent // remove comments, vendor prefixes .replace(/\\/\\*.*\\*\\/|(-moz-|-ms-|-o-|-webkit-)+\\w+(?=:)/g,"") // negate `:hover` .match(/[az-]+(?=:[^before|after|hover])/ig); console.log(props)
html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, img, ins, kbd, q, s, samp, small, strike, strong, sub, sup, tt, var, b, u, i, center, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td, article, aside, canvas, details, embed, figure, figcaption, footer, header, hgroup, menu, nav, output, ruby, section, summary, time, mark, audio, video { margin: 0; padding: 0; border: 0; font-size: 100%; font: inherit; vertical-align: baseline; } /* HTML5 display-role reset for older browsers */ article, aside, details, figcaption, figure, footer, header, hgroup, menu, nav, section { display: block; } body { line-height: 1; } ol, ul { list-style: none; } blockquote, q { quotes: none; } blockquote:before, blockquote:after, q:before, q:after { content: ''; /* duplicate `content` property here */ /* content: none; */ } table { border-collapse: collapse; border-spacing: 0; } table:hover { color:blue; -moz-animation: name 1s; }
You may not need regular expressions for this. 您可能不需要正则表达式。
You can access the stylesheet with JavaScript, then iterate over the stylesheet's cssRules
object and split each declaration at ;
您可以使用JavaScript访问样式表,然后遍历样式表的cssRules
对象并将每个声明拆分为;
then split each property/value pair at the :
character. 然后在:
字符处拆分每个属性/值对。
It's worth pointing out that this method will expand the shorthand properties since the cssText
property consists of the computed properties/values. 值得指出的是,此方法将扩展速记属性,因为cssText
属性由计算的属性/值组成。
function getPropertiesFromStylesheet(stylsheetIndex) { var stylesheet = document.styleSheets[stylsheetIndex], rules = stylesheet.cssRules, properties = []; if (stylesheet) { Object.keys(rules).forEach(function(key) { rules[key].style.cssText.split(';').forEach(function(declaration) { if (declaration) { properties.push(declaration.split(':')[0].trim()); } }); }); } return properties; } document.body.textContent = getPropertiesFromStylesheet(0);
html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, img, ins, kbd, q, s, samp, small, strike, strong, sub, sup, tt, var, b, u, i, center, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td, article, aside, canvas, details, embed, figure, figcaption, footer, header, hgroup, menu, nav, output, ruby, section, summary, time, mark, audio, video { margin: 0; padding: 0; border: 0; font-size: 100%; font: inherit; vertical-align: baseline; } /* HTML5 display-role reset for older browsers */ article, aside, details, figcaption, figure, footer, header, hgroup, menu, nav, section { display: block; } body { line-height: 1; } ol, ul { list-style: none; } blockquote, q { quotes: none; } blockquote:before, blockquote:after, q:before, q:after { content: none; } table { border-collapse: collapse; border-spacing: 0; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.