Skip to content

Instantly share code, notes, and snippets.

@tomhodgins
Last active November 3, 2020 16:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomhodgins/3e27a0ceff7a6e074c9e688dcbf59c90 to your computer and use it in GitHub Desktop.
Save tomhodgins/3e27a0ceff7a6e074c9e688dcbf59c90 to your computer and use it in GitHub Desktop.
const extractCustomProperties = (string = '') => Object.fromEntries(
Array.from(
string.matchAll(/--(?<property>[^:]+?)\s*:\s*(?<value>.*?)\s*[;}]/gus),
([match, property, value]) => [property, value]
)
)
const css = `
:root {
--custom: property;
--brand-color: #0c0;
}
`
console.log(
extractCustomProperties(css)
)
// {custom: "property", brand-color: "#0c0"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment