Remove Comma from String in JavaScript

In this post, we will see how to remove comma from String in Javascript.

There are multiple ways to do it. Let’s go through them.

Using replace()

To remove comma from String in Javascript:

  • Use replace() method with regular expression /\,/g as first parameter and empty string as second parameter.

Output:

The replace() method returns a new string with matches of pattern replaced by a replacement string.
It takes 2 parameters.

  1. Regular expression which we want to match in String.
  2. Replacement String – String with which we want to replace matched string.

Let’s go through regular expression /\,/g used in above replace method.

  • The forward slashes mark start and end of regular expression
  • We have used \ before , to escape it as , has special meaning in regular expression.
  • g denotes global replacement here. It is flag at end of regex which depicts that we want to remove all the commas not only first one.

replace() returns a new String, it does not change contents of original String.

Using replaceAll()

To remove comma from string in Javascript:

  • Use replaceAll() method with , as first parameter and empty string as second parameter. replaceAll() method returns new String with all matches replaced by given replacement.

The replaceAll() method returns a new string with matches of passed string replaced by a replacement string.
It takes 2 parameters.

  1. substring which we want to match in String.
  2. Replacement String – String with which we want to replace matched string.

Output:

replaceAll() method is not supported in Internet Explorer versions 6-11. Use replace method in case you are using them.

Using split() and join() method

To remove comma from String in Javascript:

  • Split the string by comma using split() method.
  • Join the String with empty String using empty space

This method is useful, when you want to avoid regular expressions in replace() method.

Output:

.
We used split() method to split the String on each occurrence of , into array.

Then joined the array elements with empty String using join() method.

That’s all about how to remove comma from string in Javascript.

Was this post helpful?

Related Posts

  • 28 January

    PowerShell – Get Number of Lines in CSV File

    Table of ContentsUsing Get-Content CmdletUsing Import-Csv CmdletUsing Get-ChildItem with Foreach-Object Using Get-Content Cmdlet The Get-Content cmdlet can be used in the following ways to get the number of lines in the specified .csv file: Get-Content cmdlet with .Length property Get-Content cmdlet with .Count property Get-Content cmdlet with .ReadCount property Get-Content cmdlet with Measure-Object cmdlet and […]

  • Read CSV file in PowerShell
    28 January

    Read CSV File in PowerShell

    Table of ContentsUsing the Import-Csv CmdletUsing the ConvertFrom-Csv CmdletUse the ConvertFrom-Csv Cmdlet with the Pipe (|) OperatorUse the ConvertFrom-Csv Cmdlet with the -InputObject Parameter Using the Import-Csv Cmdlet To read a CSV file in PowerShell, use the Import-Csv cmdlet. [crayon-63da5a23516c5465566509/] [crayon-63da5a23516c9794179840/] PowerShell provides several cmdlets that allow us to work with CSV files: Import-Csv ConvertFrom-Csv […]

  • Read File into String in PowerShell
    27 January

    Read File into String in PowerShell

    Table of ContentsUsing the Get-Content CmdletUse the Get-Content Cmdlet to Read a Single FileUse the Get-Content Cmdlet with forreach loop to Read Multiple FilesUsing [System.IO.File]::ReadAllText() MethodUse the [System.IO.File]::ReadAllText() Method with foreach loop to Read Multiple FilesUsing the Variable Notation with the Out-String Cmdlet Using the Get-Content Cmdlet We can use the Get-Content cmdlet to read […]

  • Get String between two characters in JavaScript
    27 January

    Get String Between Two Characters in JavaScript

    Table of ContentsUsing substring() MethodUsing slice() MethodUsing split() MethodUsing substr() Method 💡TL;DR Use the substring() method to get String between two characters in JavaScript. [crayon-63da5a23519dc093592339/] [crayon-63da5a23519df872092664/] Here, we got String between , and ! in above example. Using substring() Method Use the substring() method to extract a substring that is between two specific characters from […]

  • Remove property from Object in PowerShell
    27 January

    Remove Property from Object in PowerShell

    Table of ContentsUsing PSObject.Properties.Remove() MethodUsing -ExcludeProperty Parameter Using PSObject.Properties.Remove() Method To delete a property from an object in PowerShell, Use the New-Object cmdlet to create an object. Use the PSObject.Properties.Remove() method to remove a specific property from an object by providing the property’s name as a string parameter. [crayon-63da5a2351d33269985855/] [crayon-63da5a2351d37608915200/] In the above code, we […]

  • Create File if not exists PowerShell
    26 January

    Create File If Not Exists in PowerShell

    Table of ContentsUsing the if-else StatementUse the New-Item CmdletUse the Out-File CmdletUsing the try-catch StatementUse the New-Item CmdletUse the Out-File CmdletUsing One-Liners Using the if-else Statement We can use the if-else statement to create a file as follows: Use the New-Item Cmdlet To create a file if it does not exist in PowerShell: Use the […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.