regex - 用于匹配标签的 javascript 正则表达式
问题描述
考虑关注媒体查询。
@media screen and (max-width: 768px) {
.sd[data-s-e53caaf5-c227-4897-bb16-f957ee293e2f] {
max-width: calc(100% - 20px);
}
}
@media screen and (max-width: 480px) {
.sd[data-s-e53caaf5-c227-4897-bb16-f957ee293e2f] {
max-width: calc(100% - 20px);
}
}
我有机器生成的 html 代码。我在代码中有超过 200 多个这样的媒体查询。我想删除所有这些。
所以我尝试了 VScode 正则表达式搜索
但我的正则表达式
@media screen and \(max-width: 768px\)\s\{\n
只匹配所有媒体查询的第一行。让我知道如何使用正则表达式。建议是否有其他方法可以完成相同的任务。✌️
解决方案
您可以将文本与正则表达式匹配
^ *@media screen and \(max-width: \d{3}px\) *{ *\r?\n(?:.*\S.*\r?\n)+\r?\n?
并用空字符串替换匹配的文本。
正则表达式引擎执行以下操作。
^ * # match 0+ spaces at beginning of a line
@media screen and \(max-width: # match string
\d{3}px\) * # match 3 digits, 'px)' then 0+ spaces
{ *\r?\n # match '{', 0+ spaces, '\r' opt, '\n'
(?:.*\S.*\r?\n) # match a line with 1+ non-whitespace chars
+ # match above non-capture group 1+ times
\r?\n? # optionally match '\r' and '\n'
推荐阅读
- r - R ggplot2面板层中的多个圆角矩形
- apache-kafka - 是否有任何已实现的 SubscribableChannel/EventDrivenConsumer 可用于 Spring Integration Kafka 消费者?
- javascript - 机器人没有推动顶级用户
- android - ConstraintLayout - 将小部件连接到障碍问题
- java - 使用 jni 将返回带有模板的对象的 java 代码转换为 c++
- javascript - 有什么方法可以预测客户端的网络是否支持 WebRTC 对等连接?
- django-models - 外键返回对象而不是整数
- css - 添加一个按钮以使用 ngAfterViewInit 更新 mat 表搜索
- r - 如何获取多个 Sample() 向量输出并将它们组合成一个数据框
- javascript - 错误 er_con_count_error 连接数过多表示 mysql