分别去字符串前后,左边,右边空格
String.prototype.trim = function(){ return this.replace(/^\\s+|\\s+$/g,\"\")}
String.prototype.ltrim = function(){ return this.replace(/^\\s+/g,\"\")}
String.prototype.rtrim = function(){ return this.replace(/\\s+$/g,\"\")}
为String对象增加一个trim方法。
以后就可以这样使用:
var s = \" abc \";
s = s.trim(); // s是个String,可以使用刚定义的trim方法。
alert(s);
本文地址:https://www.stayed.cn/item/2283
转载请注明出处。
本站部分内容来源于网络,如侵犯到您的权益,请 联系我