Javascript: Remove last character if a colon

The regular expression literal (/.../) should not be in a string. Correcting your code for removing the colon at the beginning of the string, you get:

myString = myString.replace(/^\:/, '');

To match the colon at the end of the string, put $ after the colon instead of ^ before it:

myString = myString.replace(/\:$/, '');

You can also do it using plain string operations:

if (myString.charAt(myString.length - 1) == ':') {
  myString = myString.substr(0, myString.length - 1);
}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)