How to remove a character from a string using JavaScript?

Removing a character from the string is quite easy in JavaScript. We can remove a character from the string in multiple ways:

By using replace method

This will replace only the first one:

    const str = "ctl/r2021_33/bd";
    console.log(str.replace('/','-'));

By using replace with regex

This will replace only the first one:

    const str = "ctl/r2021_33/bd";
    console.log(str.replace(/\//,'-'));

This will replace all occurrences:

    const str = "ctl/r2021_33/bd";
    console.log(str.replace(/\//g,'-'));
Share