Bug Tracker

Opened 10 years ago

Closed 10 years ago

#4848 closed enhancement (wontfix)

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, "" );

}

Change History (4)

comment:1 Changed 10 years ago by dotnetCarpenter

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, "" );
}

comment:2 in reply to:  description Changed 10 years ago by dotnetCarpenter

Test Case

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

comment:3 Changed 10 years ago by aakoch

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.

comment:4 Changed 10 years ago by john

Resolution: wontfix
Status: newclosed

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

Note: See TracTickets for help on using tickets.