Skip to main content

Bug Tracker

Side navigation

#4848 closed enhancement (wontfix)

Opened July 03, 2009 06:59PM UTC

Closed March 09, 2010 02:06PM UTC

trim method doesn't trim all white-space

Reported by: dotnetCarpenter Owned by:
Priority: major Milestone: 1.4
Component: core Version: 1.3.2
Keywords: trim, patch, regex Cc:
Blocked by: Blocking:
Description

The trim method only removes white-space from the beginning and end of a string. I often need to remove all space, e.i. a telephone number ect. Therefore I suggest a second optional boolean argument called 'all'. If 'all' is true, all white-space is removed.

Test Case

var withSpace = $.trim(' 432 334 532 ');

var noSpace = $.trim(' 432 334 532 ', true);

Patch

/**

  • Uses a regular expression to remove whitespace from the given string.
  • @param {String} text The string to trim.
  • @param {Boolean} [all] True to remove all white-space from the text.
  • @return {String}

*/

trim: function(text, all) {

return all===true ? (text || "").replace( /\\s+/g, "") : (text || "").replace( /^\\s+|\\s+$/g, "" );

}

Attachments (0)
Change History (4)

Changed July 03, 2009 07:02PM UTC by dotnetCarpenter comment:1

Patch code without wiki formatting:

/**
 * Uses a regular expression to remove whitespace from the given string.
 * @param {String} text The string to trim.
 * @param {Boolean} [all] True to remove all white-space from the text.
 * @return {String}
 */
trim: function(text, all) {
  return all===true ? (text || "").replace( /\\s+/g, "") : (text || "").replace( /^\\s+|\\s+$/g, "" );
}

Changed July 05, 2009 03:11PM UTC by dotnetCarpenter comment:2

Test Case

console.log($.trim(' 432 334 532 '));
console.log($.trim(' 432 334 532 ', true));

Changed July 25, 2009 07:58PM UTC by aakoch comment:3

I don't think you understand the meaning of "trim". I would just override trim with your function. I don't think this should be a part of jQuery.

Changed March 09, 2010 02:06PM UTC by john comment:4

resolution: → wontfix
status: newclosed

This isn't something that we're going to be landing - should make for a fine plugin, though.