How to Trim a Specific Character from a String Using JavaScript like “|f|oo||” ?

Forums JavaScriptHow to Trim a Specific Character from a String Using JavaScript like “|f|oo||” ?
Staff asked 2 years ago

Can you please elaborate more for a better understanding.

Darshan Kevadiya replied 2 years ago

Answers (1)

Add Answer
Staff answered 2 years ago

Use the Replace() method :

let str = "|f|oo||";
let charToRemove = "|";
let trimmedStr = str.replace(new RegExp(charToRemove, "g"), "");

console.log(trimmedStr); // Output: "foo"

 

Subscribe

Select Categories