red-bg background-color rgba255 0 0 05; font-size 15px; font-weight bold; text-shadow 1px 1px 1px #000;用正则提取里的内容并将左右两边的数据用key与velue 生成对象
const str = ".red-bg {\n background-color: rgba(255, 0, 0, 0.5);\n font-size: 15px;\n font-weight: bold;\n text-shadow: 1px 1px 1px #000;\n}"; const regex = /{([^}]+)}/; const match = regex.exec(str); const cssProps = match[1].trim().split(";").reduce((acc, prop) => { if (prop) { const [key, value] = prop.split(":").map(str => str.trim()); acc[key] = value; } return acc; }, {}); console.log(cssProps); // {background-color: "rgba(255, 0, 0, 0.5)", font-size: "15px", font-weight: "bold", text-shadow: "1px 1px 1px #000"}
原文地址: https://www.cveoy.top/t/topic/1UU 著作权归作者所有。请勿转载和采集!