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 remove comma from a string using replaceAll()
- Javascript remove comma from a string using split() and join()
- Javascript remove first and last comma from a string
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:-
Frequently Asked:
- Javascript: Remove all but numbers from string
- Javascript: Capitalize first letter of each word in a string
- Covert All Letters of String to Lower Case in JavaScript
- Javascript: Check if string is url
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:
- Javascript remove line breaks from string (4 ways)
- Remove special characters from a string in python
- Javascript: How to remove text from string
- Javascript: Remove last character of string
- Javascript: Remove first character from a string
I hope this article helped you to delete commas from a string in javascript. Good Luck !!!