Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Transform a string that contains CSS into an object that can be used to style React components via the `style` property.
const css2obj = css => {
const regex = /(?<=^|;)\s*([^:]+)\s*:\s*([^;]+)\s*/g
const styles = {};
css.replace(regex, (_,p,value) => {
const property = p.replace(/-(.)/g, (_,p) => p.toUpperCase())
styles[property] = value
return styles;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment