The best way to resolve this task is (similar with PHP trim
function):
function trim( str, charlist ) {_x000D_
if ( typeof charlist == 'undefined' ) {_x000D_
charlist = '\\s';_x000D_
}_x000D_
_x000D_
var pattern = '^[' + charlist + ']*(.*?)[' + charlist + ']*$';_x000D_
_x000D_
return str.replace( new RegExp( pattern ) , '$1' )_x000D_
}_x000D_
_x000D_
document.getElementById( 'run' ).onclick = function() {_x000D_
document.getElementById( 'result' ).value = _x000D_
trim( document.getElementById( 'input' ).value,_x000D_
document.getElementById( 'charlist' ).value);_x000D_
}
_x000D_
<div>_x000D_
<label for="input">Text to trim:</label><br>_x000D_
<input id="input" type="text" placeholder="Text to trim" value="dfstextfsd"><br>_x000D_
<label for="charlist">Charlist:</label><br>_x000D_
<input id="charlist" type="text" placeholder="Charlist" value="dfs"><br>_x000D_
<label for="result">Result:</label><br>_x000D_
<input id="result" type="text" placeholder="Result" disabled><br>_x000D_
<button type="button" id="run">Trim it!</button>_x000D_
</div>
_x000D_
P.S.: why i posted my answer, when most people already done it before? Because i found "the best" mistake in all of there answers: all used the '+' meta instead of '*', 'cause trim
must remove chars IF THEY ARE IN START AND/OR END, but it return original string in else case.