この 100% 鉄壁の作業を行うには、おそらく CSS パーサーを作成する必要がありますが、これは簡単なことではありません。ただし、ほとんどの場合、これは正規表現で解決できます。重要なのは、1 つの正規表現で多くのことを実行しようとしないことです (よくある間違い)。私のソリューションには、2 つの別個の正規表現が含まれます。1 つはルールを解析するためのもので、もう 1 つは宣言ブロックから 'font-size' 宣言を選択するためのものです。利用可能な言語やツールについては言及されていませんが、私はこれを JavaScript で実装しました。
var css = $('style').text(); // this will grab the CSS of the page it's on...you can
// use whatever CSS you want, of course; this is just
// a string containing CSS
// join all lines by removing carriage returns; otherwise we'll run into
// lots of problems trying to grok rules that span multiple lines
css = css.replace( /\n/g, '' );
// match all rules, and separate into selectors and declarations
css = css.replace(/\s*([^{]*)\s*\{(.*?)\}/g, function(match,selector,declarations) {
// see if font-size is among the declarations; if so, capture it's value
var fsMatch = declarations.match( /\bfont-size\s*:\s*([^;]+?)\s*(;|$)/i );
// if font-size is present, return only that declaration, otherwise return empty
return fsMatch ?
selector + ' { font-size: ' + fsMatch[1] + '; }\n' :
'';
});
// now the 'css' variable contains the CSS you're looking for
このソリューションは、非常にクリーンな CSS も生成することに注意してください。
ここで実際の例を参照してください。
http://jsfiddle.net/yLjCe/2/