Skip to content

Instantly share code, notes, and snippets.

@lsauer lsauer/css2json.js
Created Aug 28, 2015

What would you like to do?
JavaScript: convert css string to JSON
//converts a css string to json
var css2json = function(str){
return str
.replace(/(\w*:)/g, '$1"') //create json format
.replace(/[;]/g, '";')
.replace(/(\'{2,})/g, '"')
.replace(/;/g, ',')
.replace(/(['"])?([a-zA-Z0-9_-]+)(['"])?:/g, '"$2": ')

This comment has been minimized.

Copy link

SensonicsDev commented Nov 21, 2018

This function does not work. Here is an example:
css2json('body{font-size:12px;}p{color:red;}'); --> "body{\"font-size\": \"12px\"}p{\"color\": \"red\",}" JSON.parse(css2json('body{font-size:12px;}p{color:red;}')); --> SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data[Learn More]


This comment has been minimized.

Copy link

mohaimenmahi commented Sep 23, 2019

Thanks for sharing. It helped much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.