[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

$ 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(/\:$/, ''))

Run here https://onecompiler.com/javascript/3xnp9rssx

3 years ago by Meera

answer

3 years ago by kim juliet

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