Wednesday, June 5, 2024
 Popular · Latest · Hot · Upcoming
72
rated 0 times [  74] [ 2]  / answers: 1 / hits: 32869  / 12 Years ago, tue, july 3, 2012, 12:00:00

I have a string like that:



var str = 'aaaaaa, bbbbbb, ccccc, ddddddd, eeeeee ';


My goal is to delete the last space in the string. I would use,



str.split(0,1);


But if there is no space after the last character in the string, this will delete the last character of the string instead.



I would like to use



str.replace(regex,'');


I am beginner in RegEx, any help is appreciated.



Thank you very much.


More From » regex

 Answers
16

Do a google search for javascript trim and you will find many different solutions.



Here is a simple one:



trimmedstr = str.replace(/s+$/, '');

[#84504] Monday, July 2, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
hakeemabramh

Total Points: 234
Total Questions: 109
Total Answers: 109

Location: Romania
Member since Mon, Jun 6, 2022
2 Years ago
;