Step 1. We perform lexical analysis , breaking the source text into tokens . Get an array like:
'.class01', '{', '.class02', '{', 'color', ':', '#000', ';', '}', '}'
Step 2. We perform the syntax analysis , making up a hierarchical structure based on the resulting array. In this example, the opening brace is the beginning of the content block, and the closing, respectively, its end. Thus, the structure will look like this:
var tree = { '.class01': { '.class02': { color: '#000' } } };
Step 3. On the basis of the data obtained, we compose a structure for our needs.
PS To build the structure of the source text, you can forget about using regular expressions , since it is difficult to keep track of the pair of opening / closing brackets in them.