首页 > 解决方案 > 正则表达式删除双引号之间的换行符而不影响其他换行符(渴望匹配)

问题描述

我有一个映射(键,值对),并且这些值中很少有换行符(在双引号内),这会导致 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",

我尝试了许多正则表达式,但它们要么删除最后两行中的换行符,要么根本不做任何事情。

提前致谢

标签: javascriptregexreplacefind

解决方案


利用

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, '')))

即仅删除引号之间的回车和换行字符。


推荐阅读