cleanup newlines in responseText
I found the following code in one of the pages I have to maintain;
document.getElementById('dummy').innerHTML = httpRequest.responseText;
return document.getElementById('dummy').innerHTML;
expecting it to do nothing, I replaced it with
return httpRequest.responseText;
but then the script choked on the input. It turned out that there were \r characters at the end of the line, so appare开发者_如何学Pythonntly the code above was to clean up the newlines.
Is there a cleaner way to do so without having to place the text in a html dummy element?
Use the string.replace
function:
return httpRequest.responseText.replace(/\r/g, "");
Use \g to replace all \r characters or else replace will remove only one \r .
httpRequest.responseText.replace(/\r/g, '');
精彩评论