Chris Dutrow Chris Dutrow - 5 years ago 333
Javascript Question

Remove trailing character(s) from string in Javascript

What is an acceptable way to remove a particular trailing character from a string?

For example if I had a string:

> "item,"

And I wanted to remove trailing ','s only if they were ','s?


Answer Source

Use a simple regular expression:

var s = "item,";
s = s.replace(/,+$/, "");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download