jasonc Posted May 30, 2011 Share Posted May 30, 2011 I have this which trims and replaces double spaces with single spaces. String.prototype.trim = function () { return this.replace(/^\s+|\s+$/g, '').replace(/\s+/g,' ') ; } I wish to have a new function that only trims the start and end of a string of its spaces. what change to the above should I make ? Quote Link to comment https://forums.phpfreaks.com/topic/237834-how-to-trim-only-start-and-end-of-string/ Share on other sites More sharing options...
requinix Posted May 30, 2011 Share Posted May 30, 2011 Remove the second .replace. Quote Link to comment https://forums.phpfreaks.com/topic/237834-how-to-trim-only-start-and-end-of-string/#findComment-1222180 Share on other sites More sharing options...
jasonc Posted May 30, 2011 Author Share Posted May 30, 2011 oh, thank you Quote Link to comment https://forums.phpfreaks.com/topic/237834-how-to-trim-only-start-and-end-of-string/#findComment-1222183 Share on other sites More sharing options...
dougjohnson Posted May 31, 2011 Share Posted May 31, 2011 I think TRIM() would do the same thing also. Quote Link to comment https://forums.phpfreaks.com/topic/237834-how-to-trim-only-start-and-end-of-string/#findComment-1222956 Share on other sites More sharing options...
requinix Posted May 31, 2011 Share Posted May 31, 2011 I think TRIM() would do the same thing also. In MySQL or PHP, sure... Quote Link to comment https://forums.phpfreaks.com/topic/237834-how-to-trim-only-start-and-end-of-string/#findComment-1223045 Share on other sites More sharing options...
Maq Posted May 31, 2011 Share Posted May 31, 2011 Yeah, I don't think Javascript has a native trim() method but JQuery does: http://api.jquery.com/jQuery.trim/ Quote Link to comment https://forums.phpfreaks.com/topic/237834-how-to-trim-only-start-and-end-of-string/#findComment-1223120 Share on other sites More sharing options...
.josh Posted June 1, 2011 Share Posted June 1, 2011 Yeah, I don't think Javascript has a native trim() method but JQuery does: http://api.jquery.com/jQuery.trim/ Yeah, no native trim method...I find that odd. Quote Link to comment https://forums.phpfreaks.com/topic/237834-how-to-trim-only-start-and-end-of-string/#findComment-1223581 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.