简体   繁体   中英

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('')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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