Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
138
rated 0 times [  143] [ 5]  / answers: 1 / hits: 36111  / 11 Years ago, sat, january 4, 2014, 12:00:00

I have this requirement where it is required to remove only the last . dot sign from a string.



Say if we have var str = 'abcd dhfjd.fhfjd.'; i need remove the final dot sign which would output abcd dhfjd.fhfjd .



I found this link ( Javascript function to remove leading dot ) which removes the first dot sign but I am new to this whole thing and could not find any source on how to remove a specific last character if exists.



Thank you :)


More From » javascript

 Answers
11

Single dot:



if (str[str.length-1] === .)
str = str.slice(0,-1);


Multiple dots:



while (str[str.length-1] === .)
str = str.slice(0,-1);


Single dot, regex:



str = str.replace(/.$/, );


Multiple dots, regex:



str = str.replace(/.+$/, );

[#73374] Friday, January 3, 2014, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tatumm

Total Points: 47
Total Questions: 92
Total Answers: 89

Location: Palau
Member since Tue, May 30, 2023
1 Year ago
;