开发者

cleanup newlines in responseText

开发者 https://www.devze.com 2023-01-09 23:02 出处:网络
I found the following code in one of the pages I have to maintain; document.getElementById(\'dummy\').innerHTML = httpRequest.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, '');

0

精彩评论

暂无评论...
验证码 换一张
取 消