[英]removing \n and \r but not \r\n
I have a string with both \r
and \n
as well as \r\n
.我有一个包含\r
和\n
以及\r\n
的字符串。
How do we remove \r
and \n
, while keeping \r\n
in tact?我们如何删除\r
和\n
,同时保持\r\n
完整?
Negative lookahead+lookbehind can do that:负前瞻+后视可以做到这一点:
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
Only the \r\n
between c
and d
are kept intact.只有c
和d
之间的\r\n
保持不变。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.