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 ? 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. 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 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. 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... 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/ 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. 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
Archived
This topic is now archived and is closed to further replies.