Javascript / convert CSS style string into JS object

后端 未结 10 1066
粉色の甜心
粉色の甜心 2021-01-04 08:41

We\'d like to convert a CSS style entered as string into a JS object.

E.g.,

 var input = \" border:solid 1px; color:red \";

expec

10条回答
  •  无人及你
    2021-01-04 09:07

    A very simple one:

    var regex = /([\w-]*)\s*:\s*([^;]*)/g;
    var match, properties={};
    while(match=regex.exec(cssText)) properties[match[1]] = match[2].trim();
    

    https://regex101.com/r/nZ4eX5/1

提交回复
热议问题