javascript - 正则表达式删除双引号之间的换行符而不影响其他换行符(渴望匹配)
问题描述
我有一个映射(键,值对),并且这些值中很少有换行符(在双引号内),这会导致 css 出现问题。如何使用正则表达式查找和替换(适用于 javascript)删除这些链接中断(仅限双引号内的链接)
输入:
actionIconTransition: " background-color 0.2s,
color 0.2s, box-shadow 0.2s",
listItemTransition:
" background-color 0.2s,
border-color 0.2s, box-shadow 0.2s",
primeIconFontSize: " 1rem",
divider: " 1px solid #dee2e6",
预期输出:
actionIconTransition: " background-color 0.2s,color 0.2s, box-shadow 0.2s",
listItemTransition:
" background-color 0.2s,border-color 0.2s, box-shadow 0.2s",
primeIconFontSize: " 1rem",
divider: " 1px solid #dee2e6",
我尝试了许多正则表达式,但它们要么删除最后两行中的换行符,要么根本不做任何事情。
提前致谢
解决方案
利用
let test_str = `actionIconTransition: " background-color 0.2s,
color 0.2s, box-shadow 0.2s",
listItemTransition:
" background-color 0.2s,
border-color 0.2s, box-shadow 0.2s",
primeIconFontSize: " 1rem",
divider: " 1px solid #dee2e6",`
console.log(test_str.replace(/"[^"]*"|'[^']*'/g, (z) => z.replace(/[\r\n]+/g, '')))
即仅删除引号之间的回车和换行字符。
推荐阅读
- c++ - 如何从插件中的单独 C++ 线程调用发射器回调?
- html - 如何在 HTML/CSS 中为页面上的所有内容设置边框/边距?
- sql-server - Sql Server:如何从存储过程中为非特权用户创建数据库快照?
- ansible - Ansible 2.7:如何列出未归档的文件
- javascript - Firestore - 查询,然后更新
- java - 如何洗牌存储在数组中的一副牌?
- rasa-core - 如何将实体识别与意图预测联系起来?
- php - POST 数据中的 CURLFile 对象使我的请求失败
- c++ - 使用行顺序在打印二维数组时显示垃圾值
- php - 在 PHP 中传递逗号分隔的字符串值