javascript - 删除 \n 和 \r 但不删除 \r\n
问题描述
我有一个字符串同时包含\r
和\n
以及\r\n
。
我们如何删除\r
and \n
,同时保持\r\n
原样?
解决方案
负前瞻+后视可以做到这一点:
const test="a\nb\rc\r\nd\n\re";
console.log(test.replaceAll(/(\r(?!\n)|((?<!\r)\n))/g,"x"));
// ^^^^^^^^ match \r if it has no \n after
// ^^^^^^^^^^^ match \n if it has no \r before
只有\r\n
介于c
和d
之间保持完整。
推荐阅读
- hive - HIVE中的两个表比较
- java - 如何在 Java for Android 中通过多个顺序的、依赖的 AsyncTasks 引用变量?
- python - 无法将函数输出分配给列
- reactjs - 在状态中保存范围滑块值并在单击按钮时发起 axios 请求
- react-native - 反应导航选项卡不可点击的绝对位置
- excel - 为什么我不能使用excel得到矩阵的逆?
- java - 每次我从 jtextfield 窗口检查用户的输入时,当我移动整个图形窗口时,结果只会打印在我的图形窗口上
- angular - 发生错误:从角度 7 调用 http Post 时解析 [URL] 时出现 Http 失败
- python-3.x - 使用 bs4 抓取动态内容
- r - 有没有一种方法可以在不重新缩放任何 y 轴的情况下覆盖两个 ggplot 组面板?