Trim certain characters from a string in JavaScript
There are many different ways to tackle trimming strings, a regex is probably the most popular, and there are lots of blogs and performance tests out there, (one such blog), that you can look at and decide which is best.

So continuing along the lines that you have demonstrated, then this may be of some help to you.

Javascript

/*jslint maxerr: 50, indent: 4, browser: true */

var trim = (function () {
    "use strict";

    function escapeRegex(string) {
        return string.replace(/[\[\](){}?*+\^$\\.|\-]/g, "\\$&amp;");
    }

    return function trim(str, characters, flags) {
        flags = flags || "g";
        if (typeof str !== "string" || typeof characters !== "string" || typeof flags !== "string") {
            throw new TypeError("argument must be string");
        }

        if (!/^[gi]*$/.test(flags)) {
            throw new TypeError("Invalid flags supplied '" + flags.match(new RegExp("[^gi]*")) + "'");
        }

        characters = escapeRegex(characters);

        return str.replace(new RegExp("^[" + characters + "]+|[" + characters + "]+$", flags), '');
    };
}());

/*jslint devel: true */

console.log(trim(" - hello** +", "+*- "));


On jsfiddle

Of course, you can customise which characters you wish to escape, or flags to test for, as per your own needs. Regular Expressions