admin管理员组文章数量:1301503
$.trim(value);
The above jquery code would trim the text. I need to trim the string using Javascript.
I tried:
link_content = " check ";
trim_check = link_content.replace(/(?:(?:^|\n)\s+|\s+(?:$|\n))/g,'').replace(/\s+/g,'');
How to use trim in javascript? Equivalent of $.trim()
of jQuery?
$.trim(value);
The above jquery code would trim the text. I need to trim the string using Javascript.
I tried:
link_content = " check ";
trim_check = link_content.replace(/(?:(?:^|\n)\s+|\s+(?:$|\n))/g,'').replace(/\s+/g,'');
How to use trim in javascript? Equivalent of $.trim()
of jQuery?
3 Answers
Reset to default 10JavaScript 1.8.1 includes the trim method on String objects. This code will add support for the trim method in browsers that do not have a native implementation:
(function () {
if (!String.prototype.trim) {
/**
* Trim whitespace from each end of a String
* @returns {String} the original String with whitespace removed from each end
* @example
* ' foo bar '.trim(); //'foo bar'
*/
String.prototype.trim = function trim() {
return this.toString().replace(/^([\s]*)|([\s]*)$/g, '');
};
}
})();
From the jquery source:
// Used for trimming whitespace
trimLeft = /^\s+/,
trimRight = /\s+$/,
// Use native String.trim function wherever possible
trim: trim ?
function( text ) {
return text == null ?
"" :
trim.call( text );
} :
// Otherwise use our own trimming functionality
function( text ) {
return text == null ? "" : text.toString().replace( trimLeft, "" ).replace( trimRight, "" );
},
Here are some functions that might help try them:
http://www.webtoolkit.info/javascript-trim.html
本文标签: jqueryEquivalent for trim in JavascriptStack Overflow
版权声明:本文标题:jquery - Equivalent for $.trim in Javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741677486a2391968.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论