兼容性:IE9以上、FF、chrome在换行处匹配/\n/
IE7-8在换行处先匹配/\r/,再匹配/\n/
html:
<textarea name="" id="test_new_line" cols="30" rows="10"></textarea>
<input type="button" id="submit" value="提交"/>
<div id="result"></div>
js:
document.getElementById("submit").onclick = function(){
var newString = document.getElementById("test_new_line").value.replace(/\n/g, '_@').replace(/\r/g, '_#');
document.getElementById("result").innerHTML = newString;
};
兼容性解决方案:
newString = newString.replace(/_#_@/g, '<br/>'); //IE7-8
newString = newString.replace(/_@/g, '<br/>');//IE9、FF、chrome
newString = newString.replace(/\s/g,' ');//空格处理