我有两个字符串\r
和\n
以及\r\n
。
我们如何删除\r
和\n
,同时保持\r\n
机智?
Negative lookahead+lookbehind 可以做到这一点:
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
保持完好。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句