我正在尝试在 javascript 的 window.open().document 中使用换行符 ("\n")。这没有按预期工作。 “\” 反斜杠基本上导致了它对我的代码起到中断作用的问题。我必须有这部分代码来获取换行符的计数并替换不必要的换行符。如何让“\”被视为字符串?
我引用解决方案尝试了加四个反斜杠(\\)等解决方案,但没有任何变化Can't escape the backslash with regex?
printWindow=window.open("","","location=no,menubar=no,directories=no,scrollbars=yes,toolbar=no,resizable=yes",false);
printWindow.document.write('var commentCharLimit = 6000;');
printWindow.document.write('for(var x = 0; x < textareas.length; x++)');
printWindow.document.write('{');
printWindow.document.write('if(textareas[x].className.indexOf("lh-advisor-comment-input") > -1){');
printWindow.document.write('var textareaContent = textareas[x].value;');
// this is where my code is getting failed because of the "\" used
printWindow.document.write('textareaContent = textareaContent.replace(/(\r\n|\n|\r)/gm,"");');
printWindow.document.write('var truncatedTextareaContent = textareaContent.substring(0, commentCharLimit);');
printWindow.document.write('textareas[x].value = truncatedTextareaContent;');
printWindow.document.write('var maxrows=300; ');
//printWindow.document.write(' textareas[x].rows=50;');
printWindow.document.write(' var cols=textareas[x].cols; ');
// also here
printWindow.document.write('var arraytxt=truncatedTextareaContent.split("\\n");');
printWindow.document.write('var rows=arraytxt.length; ');
printWindow.document.write('for (i=0;i<arraytxt.length;i++) ');
printWindow.document.write('rows+=parseInt(arraytxt[i].length/cols)');
printWindow.document.write(' if (rows>maxrows) textareas[x].rows=maxrows;');
printWindow.document.write(' else textareas[x].rows=rows;');
printWindow.document.write('}');
printWindow.document.write('}');
如果我在 printwindow.document 之外尝试相同的代码,我希望输出考虑“/”并执行它,它工作正常。
最佳答案
需要转义才能被忽略。当我在控制台中运行它时,以下工作正常,我在输出中看到 \r\n|\n|\r
。
printWindow=window.open("","","location=no,menubar=no,directories=no,scrollbars=yes,toolbar=no,resizable=yes",false);
printWindow.document.write('var commentCharLimit = 6000;');
printWindow.document.write('for(var x = 0; x < textareas.length; x++)');
printWindow.document.write('{');
printWindow.document.write('if(textareas[x].className.indexOf("lh-advisor-comment-input") > -1){');
printWindow.document.write('var textareaContent = textareas[x].value;');
printWindow.document.write('textareaContent = textareaContent.replace(/(\\r\\n|\\n|\\r)/gm,"");');
printWindow.document.write('var truncatedTextareaContent = textareaContent.substring(0, commentCharLimit);');
printWindow.document.write('textareas[x].value = truncatedTextareaContent;');
printWindow.document.write('var maxrows=300; ');
//printWindow.document.write(' textareas[x].rows=50;');
printWindow.document.write(' var cols=textareas[x].cols; ');
// also here
printWindow.document.write('var arraytxt=truncatedTextareaContent.split("\\\\n");');
printWindow.document.write('var rows=arraytxt.length; ');
printWindow.document.write('for (i=0;i<arraytxt.length;i++) ');
printWindow.document.write('rows+=parseInt(arraytxt[i].length/cols)');
printWindow.document.write(' if (rows>maxrows) textareas[x].rows=maxrows;');
printWindow.document.write(' else textareas[x].rows=rows;');
printWindow.document.write('}');
printWindow.document.write('}');
关于javascript - 换行符在 window.open().document 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56994435/