Skip to main content

Trim the leading and trailing comma from string in Javascript

You can trim leading and trailing comma from string in Javascript. This code snippet will help you to remove the first and last comma from string using replace method with regex on string.

  • Removing leading and trailing comma from string in Javascript

    const str = ",samar, w3codegenerator,";
    const output = str.replace(/(^,)|(,$)/g, "");
    console.log(output);

    Output:

    samar, w3codegenerator

    This code snippet will help you to trim the first and last comma from string using replace method on string using javascript.

  • Back to code snippet queries related javascript