fubo fubo - 1 year ago 108
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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download