Just wondering if anyone has a tried & tested regex to parse a css font string into its various pieces:
- 12px arial
- italic bold sans-serif
- 12px/50px verdana
- etc
Just wondering if anyone has a tried & tested regex to parse a css font string into its various pieces:
Answering my own question:
/^\s*(?=(?:(?:[-a-z]+\s*){0,2}(italic|oblique))?)(?=(?:(?:[-a-z]+\s*){0,2}(small-caps))?)(?=(?:(?:[-a-z]+\s*){0,2}(bold(?:er)?|lighter|[1-9]00))?)(?:(?:normal|\1|\2|\3)\s*){0,3}((?:xx?-)?(?:small|large)|medium|smaller|larger|[.\d]+(?:\%|in|[cem]m|ex|p[ctx]))(?:\s*\/\s*(normal|[.\d]+(?:\%|in|[cem]m|ex|p[ctx])))?\s*([-,\"\sa-z]+?)\s*$/i
which separates to:
var parts = rx.exec( str )
, fontStyle = parts[1] || 'normal'
, fontVariant = parts[2] || 'normal'
, fontWeight = parts[3] || 'normal'
, fontSize = parts[4]
, lineHeight = parts[5]
, fontFamily = parts[6]
;
And yes, I realize that's insane
you mean like this?
How to parse CSS font shorthand format
Alternatively, there is also JSCSSP, a JavaScript library for parsing CSS.