我已经将line和lineCount方法实现为String原型:
String.prototype.lines = function() { return this.split(/r*n/); }String.prototype.lineCount = function() { return this.lines().length; }显然,在IE9中split方法不会在字符串的末尾(或textarea的innerText属性)计算回车符和/或换行符,但在Chrome
22中会对其进行计数,从而产生不同的结果。
到目前为止,当浏览器不是Internet Explorer时,我已经通过从行数中减去1来做到这一点:
String.prototype.lineCount = function() { return this.lines().length - navigator.userAgent.indexOf("MSIE") != -1); }希望有人有更好的RegExp或其他解决方法。



