javascript - 如何在javascript中分离css代码块?
问题描述
如何在javascript中分离css代码块?
以下是序列化形式的一些 css:
const css = "body { background-color: lightblue; } h1 { color: white; text-align: center; } p { font-family: verdana; font-size: 20px; }"
我如何解析它并以以下形式作为数组获取它(以及添加空格(保留空格。即格式化形式):
const parsedCss = [
'body {
background-color: lightblue;
}',
'h1 {
color: white;
text-align: center;
}',
'p {
font-family: verdana;
font-size: 20px;
}',
]
解决方案
你可以使用const parsedCss = css.split(/(?<=\})/)
这会拆分“}”符号上的 css,并使用前瞻断言来保留它。
输出将是
[
'body {
background-color: lightblue;
}',
'h1 {
color: white;
text-align: center;
}',
'p {
font-family: verdana;
font-size: 20px;
}',
]
推荐阅读
- react-native - 拖动手指时取消按钮单击
- jquery - 如何使用jQuery动画显示循环中的下一个div元素
- ios - “BackgroundModes” - iOS 项目的“project.pbxproj”文件中未列出系统功能
- docker - docker macvlan 外部子网 pfsense 其他子网
- qemu - virt-aa-helper 不在 AppArmor 生成的规则中添加存储池的路径
- css - 不应用针对不同像素宽度的 CSS 查询
- laravel-7 - 在 Laravel 7 中登录后阻止用户访问访客页面
- google-apps-script - 一张表中多个选项卡的多列(特定列)的时间戳脚本
- python - 我正在学习基本的 Python。我无法执行此代码
- java - java - 如何使用printf在java中的列中打印?