[Javascript] How to remove the last character of a string if it's a colon
I want to remove the last character of a string if it's a colon using regex. How to do that in Javascript?
3 Answers
3 years ago by Jahaan
$
represents end of the string. You can use replace function to remove :
if it's present as below.
Usage:
string.replace(/\:$/, '')
Example:
let str = 'hello:';
console.log(str.replace(/\:$/, ''))
3 years ago by Meera
I personally think regex is horriible
Use
string= st.toString();
if (string [string.length -1]==":") {
console.info ( string.substring ( 0, string.length-1 ))}
3 years ago by lameh 7