如何删除字符串末尾的换行符?我可以使用RegEx或string.indexOf().
到目前为止,我所做的不起作用:
var message = "first paragraph.\n\nNext paragraph.\n";
var trimMessage = message.lastIndexOf("\n")==0 ? message.substring(0, message.length-2) : message;
救援的正则表达方式:
var trimMessage = message.replace(/\n$/, '');
的$
意思是"输入结束".
例:
var message = "first paragraph.\n\nNext paragraph.\n";
var trimMessage = message.replace(/\n$/, '');
var pre = document.createElement('pre');
pre.innerHTML =
"Message:\n***" + message + "**\n\ntrimMessage = ***\n" + trimMessage + "***";
document.body.appendChild(pre);