Modify ↓
Ticket #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: | |
| Blocking: | Blocked by: |
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
comment:2 in reply to: ↑ description Changed 3 years ago by dotnetCarpenter
Test Case
console.log($.trim(' 432 334 532 '));
console.log($.trim(' 432 334 532 ', true));
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.
Note: See
TracTickets for help on using
tickets.

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