Need to do a right trim on ajax query in javascrip

2019-03-04 10:23发布

问题:

In JavaScript, I wanted to do the right trim for all the special characters at the end of the string. My Piece of code doing the trim for hyphen (-) only..Need for all the special characters(:.,()+-=/[]\@#$%^&*) as well.

var s = "DB-";
var x = s.replace(/-+$/,'');
console.log(x);

Any help on this?

回答1:

This should do the trick.

var x = "abcde#$@asbd#$^@$";
x.slice(0,x.search(/\W*$/));

This will only trim off the special characters at the end of the string. It will leave special characters that are in the body of the string.