fubo fubo - 9 months ago 46
Javascript Question

Trim specific character from a string

what's the JavaScript equivalent to this c# Method:

var x = "|f|oo||";
var y = x.Trim(new char[] { '|' }); //y == f|oo

i updated the code with f|oo as result - c# trims the selected character only at the beginning and end of the string!

Answer Source

One line is enough:

var x = '|f|oo||';
var y = x.replace(/^\|+|\|+$/g, '');
document.write(x + '<br />' + y);

^\|+   beginning of the string, pipe, one or more times
|      or
\|+$   pipe, one or more times, end of the string