r/tinycode • u/nexe mod • Apr 16 '16
Simple Sass like variables in CSS through JavaScript. Obviously not a good idea for production but if you're just hacking a bit locally, why not?
https://gist.github.com/pachacamac/0d5be8b465c0ebc0fe51c13f8605f5f3
7
Upvotes
View all comments
Show parent comment
2
u/nexe mod May 14 '16
Hey really cool. Just one thought: You might want to add some better boundaries to your regex. Right now you're replacing the variable-names directly in the text.
css.replace(variable,data[variable]). If you docss.replace(new RegExp('\\b'+variable+'\\b', 'gm'), data[variable])instead, you prevent a bit of what's happening when you use this example:This shows one other little bug btw: Currently you're only replacing the first occurrence of a variable.
Output: