Remove comma from string in javascript

Often, we come across a requirement to remove the commas from a string in javascript to operate them. This article will remove all commas from a string in javascript using different methods and examples.

Table of Contents:-

Javascript remove comma (‘,’) from a string using replace() and RegExp

Javascript’s replace() method finds a pattern and replaces some or all of its occurrences with a replacement(character/string). The pattern can be a character or a string, or regExp.

RegExp is the regular expression object. Theses objects are patterns used to match character combinations in strings.

Example:-

Remove all commas (‘,’) from the string “23,67,89,990,”

Code:-

const dummyString = "23,67,89,990,"
let stringWithoutComma =  dummyString.replace(/,/g,'') 
console.log("Original String: "+ dummyString )
console.log("Final String: "+ stringWithoutComma)

Explanation:-

The above code is using replace() method with RegExp. The regular expression is the first argument. This regular expression (/,/g) searches for all comma patterns in string and replaces them with the replacement. The second argument is the replacement, nothing (”) in our case.

Output:-

Original String: 23,67,89,990,
Final String: 236789990

Javascript remove comma (‘,’) from a string using replaceAll()

Javascript’s replaceAll() method will replace all the pattern matches within a string with a replacement.

Argument List:

First argument: It is a pattern that can be a string or a RegExp to be searched within a string.

Second argument: It is a replacement that can be a string or a function.

Example:-

Remove all commas (‘,’) from the string “23,67,89,990,”

Code:-

const dummyString = "23,67,89,990,"
let stringWithoutComma =  dummyString.replaceAll(',','') 
console.log("Original String: "+ dummyString )
console.log("Final String: "+ stringWithoutComma)

Explanation:-

Here the above code uses the replaceAll() method to replace all the occurrences of comma (,). The comma (‘,’) is passed as the first argument, and replacement is passed as the second argument, nothing (”) in our case. 

Output:-

Original String: 23,67,89,990,
Final String: 236789990

Javascript string remove all line breaks using split() and join()

The split() method in javascript returns an array of substrings formed by splitting a given string.

The join() method in javascript joins the elements of the array back into a string.

Example:-

Remove all commas (‘,’) from the string “23,67,89,990,”

Code:-

const dummyString = "23,67,89,990,"
let stringWithoutComma =  dummyString.split(',').join('')
console.log("Original String: "+ dummyString )
console.log("Final String: "+ stringWithoutComma)

Explanation:-

The above code shows that we split the original string based on comma (‘,’) into an array of substrings using the split() method. Then join the array back into one string using the join() method.

Output:-

Original String: 23,67,89,990,
Final String: 236789990

Javascript string remove first and last comma from a string

Example:-

Remove all commas (‘,’) from the string “,23,67,89,990,”

Code:-

const dummyString = ",23,67,89,990,"
let finalString =  dummyString.replace(/^,|,$/g, "")
console.log("Original String: "+ dummyString )
console.log("Final String: "+ finalString)

Explanation:-

The above code uses the replace() method to remove the first and last comma from the string. Here regular expression ( /^,|,$/g) is passed as first argument. Character ^ matches the beginning of input, and character $ matches the end of the input. The replace() method with regular expression /^,|,$/g should replace the comma at the beginning and the end of the input with (”).

Output:-

Original String: ,23,67,89,990,
Final String: 23,67,89,990

Read More:

I hope this article helped you to delete commas from a string in javascript. Good Luck !!!

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top