简体   繁体   English

删除 \n 和 \r 但不删除 \r\n

[英]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.只有cd之间的\r\n保持不变。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM