简体   繁体   English

删除换行符失败

[英]remove line breaks fails

I am trying to remove string line breaks, and i am getting the exact string. 我正在尝试删除字符串换行符,并且我正在获取确切的字符串。

this is my code 这是我的代码

function removeBreaks(string){
   let functionString = string.substring(7, string.length);
   let cleanFunction = functionString.replace(/\r?\n|\r/gm, '');
   console.log(cleanFunction);
}

let string = "dksldt: function (global){\n var o = {id:l._id, z: s, t:o};\n  return o; \n} "
removeBreaks(string);


//output: "dhslkf: function (global){\n var o = {id:l._id, z: s, t:o};\n  return o; \n}"

but when im doing: 但是当我做的时候:

"function (global){\n var o = {id:l._id, z: s, t:o};\n  return o; \n}".replace(/\r?\n|\r/gm, '');
//output: "function (global){ var o = {id:l._id, z: s, t:o};  return o; } "

in the console im getting the a good result. 在控制台即时通讯取得良好的结果。

看起来您的正则表达式是错误的,因为您忘记了\\n之前的转义斜杠:

functionString.replace(/\r?\\n|\r/gm, '');
cleanFunction.split('\n').join('')

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

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