Question: What's the **JavaScript** equivalent to this `C#` Method:

```
var x = "|f|oo||"; 
var y = x.Trim('|'); //  "f|oo"

```

C# trims the selected character only at the **beginning** and **end** of the string!

Answer: 
If you define these functions in your program, your strings will have an upgraded version of `trim` that can trim all given characters:

```js
String.prototype.trimLeft = function(charlist) {
 if (charlist === undefined)
 charlist = "\s";

 return this.replace(new RegExp("^[" + charlist + "]+"), "");
};

String.prototype.trim = function(charlist) {
 return this.trimLeft(charlist).trimRight(charlist);
};

String.prototype.trimRight = function(charlist) {
 if (charlist === undefined)
 charlist = "\s";

 return this.replace(new RegExp("[" + charlist + "]+$"), "");
};

var withChars = "/-center-/"
var withoutChars = withChars.trim("/-")
document.write(withoutChars)
```

Source
------

<https://www.sitepoint.com/trimming-strings-in-javascript/>
I would suggest looking at lodash and how they implemented the `trim` function.

See [Lodash Trim](https://lodash.com/docs/4.17.11#trim) for the documentation and [the source](https://github.com/lodash/lodash/blob/4.17.11/lodash.js#L14951) to see the exact code that does the trimming.

I know this does not provide an exact answer your question, but I think it's good to set a reference to a library on such a question since others might find it useful.