如何在字串中捕獲css規則塊?這對我來說太難捕捉了,沒有新行
.fa { color: red; }
small { font-size: 16px; }
#content { background-color: blue; }
我想要完成的是將每個塊放入陣列
['.fa { color: red; }', 'small { font-size: 16px; }', '#content { background-color: blue; }']
這在javascript正則運算式中可能嗎?
更新
對不起,我認為這只能用正則運算式解決,但得到了一個更簡單的答案,謝謝
uj5u.com熱心網友回復:
用右花括號拆分:
const str = `.fa { color: red; }
small { font-size: 16px; }
#content { background-color: blue; }
`
const res = str.split("}").map(e => e = "}")
console.log(res)
uj5u.com熱心網友回復:
const css = `.fa { color: red; }
small { font-size: 16px; }
#content { background-color: blue; }`;
const splitCSS = css.split(/\n/g);
有一個更簡單的方法...
uj5u.com熱心網友回復:
這個正則運算式可能有效。
const css = '.fa { color: red; } small { font-size: 16px; } #content { background-color: blue; }';
const result = css.match(/[^{}] \{[^}] \}/gm);
console.log(result)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/352335.html
標籤:javascript css
