Trim the leading and trailing comma from string in Javascript

Created at 02-Nov-2021 , By samar

Trim the leading and trailing comma from string in Javascript

In this session, we will try our hand at solving the "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

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

    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

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Buy Me A Coffee

Don't forget to share this article! Help us spread the word by clicking the share button below.

We appreciate your support and are committed to providing you valuable and informative content.

We are thankful for your never ending support.