Remove any number of characters from end of a string

I have a string which can have any number of asterisk () at the end and i would like to remove all and replace it with one space. E.g . " 123456789** " and i want the output to be *123456789. I have been trying to look for a regex bit can’t really find one.
Can anyone suggest anything please?

Why not splice it at the index of the asterisk, and simply append the chars you want? Really no need for regex at all.

I would not know the index as the string could be of any length which is why i was hoping regex could save me!!

str.replace(/\**/g, ' ')

This will remove any asterisk present at the beginning of the string which i don’t want. I just to remove all asterisk from he end

A great conversation on SO about this very subject:

Search only at the end

str.replace(/\*+$/g, ' ')
1 Like

Thank you so much this works!!!