You can do something like this:
"/installers/services/".replace(/^\/+/g,'').replace(/\/+$/g,'')
This regular expression is the usual way to have the same trim function behavior used in many languages.
Possible implementation of the trimmer function:
function trim(string, char){ if(!char) char = ' '; //space by default char = char.replace(/([()[{*+.$^\\|?])/g, '\\$1'); //escape char parameter if needed for regex syntax. var regex_1 = new RegExp("^" + char + "+", "g"); var regex_2 = new RegExp(char + "+$", "g"); return string.replace(regex_1, '').replace(regex_2, ''); }
All / at the beginning and at the end of the line will be deleted. It handles cases like ///installers/services///
You can also simply:
"/installers/".substring(1, string.length-1);
OlivierH Nov 25 '13 at 14:57 2013-11-25 14:57
source share