The third parameter of the String.prototype.replace() function was never defined as a standard, so most browsers simply do not implement it. It was eventually removed and replaced with String.prototype.replaceAll() (see below).


Modern solution (2022)

Use String.prototype.replaceAll(). It is now supported in all browsers and NodeJS.

var myStr = "this,is,a,test";
var newStr = myStr.replaceAll(",", "-");

console.log( newStr );  // "this-is-a-test"
Run code snippetEdit code snippet Hide Results Copy to answer Expand


The old way is to use a regular expression with g (global) flag

var myStr = "this,is,a,test";
var newStr = myStr.replace(/,/g, "-");

console.log( newStr );  // "this-is-a-test"
Run code snippetEdit code snippet Hide Results Copy to answer Expand

Have issues with regular expressions?

It is important to note, that regular expressions use special characters that need to be escaped. For example, if you need to escape a dot (.) character, you should use /\./ literal, as in the regex syntax a dot matches any single character (except line terminators).

If you need to pass a variable as a replacement string, instead of using regex literal you may create a RegExp object and pass a string as its first argument. The usual escape rules (preceding special characters with \ when included in a string) will be necessary.

Show code snippet

function escapeRegex(str) {
    return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); // $& means the whole matched string
}

var myStr = "this.is.a.test";
var reStr = escapeRegex(".");
var newStr = myStr.replace(new RegExp(reStr, "g"), "-");

console.log( newStr );  // "this-is-a-test"
Run code snippetEdit code snippet Hide Results Copy to answer Expand

Answer from VisioN on Stack Overflow
Top answer
1 of 3
904

The third parameter of the String.prototype.replace() function was never defined as a standard, so most browsers simply do not implement it. It was eventually removed and replaced with String.prototype.replaceAll() (see below).


Modern solution (2022)

Use String.prototype.replaceAll(). It is now supported in all browsers and NodeJS.

var myStr = "this,is,a,test";
var newStr = myStr.replaceAll(",", "-");

console.log( newStr );  // "this-is-a-test"
Run code snippetEdit code snippet Hide Results Copy to answer Expand


The old way is to use a regular expression with g (global) flag

var myStr = "this,is,a,test";
var newStr = myStr.replace(/,/g, "-");

console.log( newStr );  // "this-is-a-test"
Run code snippetEdit code snippet Hide Results Copy to answer Expand

Have issues with regular expressions?

It is important to note, that regular expressions use special characters that need to be escaped. For example, if you need to escape a dot (.) character, you should use /\./ literal, as in the regex syntax a dot matches any single character (except line terminators).

If you need to pass a variable as a replacement string, instead of using regex literal you may create a RegExp object and pass a string as its first argument. The usual escape rules (preceding special characters with \ when included in a string) will be necessary.

Show code snippet

function escapeRegex(str) {
    return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); // $& means the whole matched string
}

var myStr = "this.is.a.test";
var reStr = escapeRegex(".");
var newStr = myStr.replace(new RegExp(reStr, "g"), "-");

console.log( newStr );  // "this-is-a-test"
Run code snippetEdit code snippet Hide Results Copy to answer Expand

2 of 3
185

Just for fun:

var mystring = "this,is,a,test"  
var newchar = '|'
mystring = mystring.split(',').join(newchar);
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Removing commas in a string - JavaScript - The freeCodeCamp Forum
September 18, 2019 - Hey guys, i have a basic js question here… how can i remove commas in a string and replace them with a space. let myString = 'hello,this,is,a,difficult,to,read,sentence';
Discussions

Replacing space and comma from a string using regex
I may get a string like this: 23,45,567 23,45 56 7 I want it to free of all the commas and spaces. So, I tried to use the following codes: “23,45,567”.replace(‘/[1]$/g’, ‘’) “23,45,567”.replace(‘/[2]+$/g’, ‘’) “23,45,567”.replace(‘/^\s,$/g’, ‘’) And many ... More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
3
0
October 26, 2020
use replace() to remove all commas from string
Note: This feature may not be available in some browsers. ... You are using an out of date browser. It may not display this or other websites correctly. You should upgrade or use an alternative browser. ... Not open for further replies. ... How do I use replace() to remove all commas from string. More on tek-tips.com
🌐 tek-tips.com
3
0
October 10, 2005
lightning aura components - Remove multiple commas between two strings - Salesforce Stack Exchange
Maximum number of commas between the two strings will be 10. I request the members to let me know how to achieve this. ... What have you tried? Where are you stuck? Please take a look at How to Ask and edit your post. ... That's basic JavaScript. It doesn't really even matter how many commas are there, 2 or a million. Just replace them all ... More on salesforce.stackexchange.com
🌐 salesforce.stackexchange.com
Remove multiple commas at end of string?
What do you mean when you say 'trailing "," at the end as expected'? I wouldn't expect trailing commas from your example code. To replace/remove substrings use sub or gsub... More on reddit.com
🌐 r/rstats
4
1
November 29, 2022
🌐
Medium
frontendinterviewquestions.medium.com › how-to-remove-comma-from-string-in-javascript-0b5cc90fd65f
How to remove comma from string in JavaScript | by Pravin M | Medium
April 15, 2024 - The most straightforward approach to remove commas from a string in JavaScript is by using the replace method along with a regular expression. const stringWithCommas = "1,000,000"; const stringWithoutCommas = stringWithCommas.replace(/,/g, ""); ...
🌐
freeCodeCamp
forum.freecodecamp.org › programming › javascript
Replacing space and comma from a string using regex - JavaScript - The freeCodeCamp Forum
October 26, 2020 - I may get a string like this: 23,45,567 23,45 56 7 I want it to free of all the commas and spaces. So, I tried to use the following codes: “23,45,567”.replace(‘/[1]$/g’, ‘’) “23,45,567”.replace(‘/[2]+$/g’, ‘’) “23,45,567”.replace(‘/^\s,$/g’, ‘’) And many ...
🌐
TutorialsPoint
tutorialspoint.com › article › replace-commas-with-javascript-regex
Replace commas with JavaScript Regex?
All commas: My - Favorite - subject - is - First comma: My - Favorite, subject, is, Comma + space: My -Favorite -subject -is, Regular expressions provide powerful pattern matching for replacing commas in JavaScript strings.
🌐
Crio
crio.do › blog › how-to-replace-all-occurrences-of-a-string-in-javascript-2024-criodo
How Do I Replace All Occurrences of a String in JavaScript?
December 9, 2024 - The while loop runs until "abc" is no longer found in the string. Each iteration removes one occurrence of "abc" using replace(). To replace all occurrences of a substring in JavaScript:
Find elsewhere
🌐
Tek-Tips
tek-tips.com › home › forums › software › programmers › web development › javascript
use replace() to remove all commas from string | Tek-Tips
October 10, 2005 - How do I use replace() to remove all commas from string. var my_string = "This is a string, it has a comma"; my_string = my_string.replace(",",""); ? T.I.A
🌐
LinkedIn
linkedin.com › pulse › how-remove-comma-from-string-javascript-xaufc
How to remove comma from string in JavaScript
March 13, 2024 - ->The first argument is a regular expression (/,/g). This expression matches any comma character (",") and the g flag ensures all occurrences are replaced. -> The second argument is an empty string ("").
🌐
Bobby Hadz
bobbyhadz.com › blog › javascript-remove-all-commas-from-string
Remove/Replace all Commas from a String in JavaScript | bobbyhadz
The replaceAll() method will remove all commas from the string by replacing them with empty strings.
🌐
Reddit
reddit.com › r/rstats › remove multiple commas at end of string?
r/rstats on Reddit: Remove multiple commas at end of string?
November 29, 2022 -

I've been looking online to solve this problem but can't seem to get it right.

Basically I'm doing a paste0() on 4 different columns.The code looks something like this:

mutate(new_col = paste0(col1, ",", col2, ", ",col3, ",",col4))

The paste is working fine but im getting trailing "," at the end (as expected) but i can't seem to find a way to remove them after-the-fact.

I've tried df$new_col <- trimws(df$new_col,whitespace=",") but thats not working either.

Any advice on how to make progress on this?

🌐
Itsourcecode
itsourcecode.com › home › how to remove commas from string javascript? 3 methods
How To Remove Commas From String JavaScript? 3 Methods - Itsourcecode.com
May 13, 2026 - Take the following steps to utilize ... outcome: One of the simplest approaches is to use the replace() method along with a regular expression to target all commas in the string....
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › replaceAll
String.prototype.replaceAll() - JavaScript | MDN
The replaceAll() method of String values returns a new string with all matches of a pattern replaced by a replacement. The pattern can be a string or a RegExp, and the replacement can be a string or a function to be called for each match. The original string is left unchanged.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › replace
String.prototype.replace() - JavaScript | MDN
If pattern is an object with a Symbol.replace method (including RegExp objects), that method is called with the target string and replacement as arguments. Its return value becomes the return value of replace().
🌐
JavaScript in Plain English
javascript.plainenglish.io › how-to-remove-a-leading-comma-from-a-javascript-string-e703db36d975
How to Remove a Leading Comma from a JavaScript String | JavaScript in Plain English
June 16, 2022 - We can use the JavaScript string substring method to get the part of a string. Therefore, we can use it to return a string without the leading comma. ... const myOriginalString = ",'first string','more','even more'"; const newString = ...
🌐
Java2s
java2s.com › Tutorials › Javascript › jQuery_Data_Type_How_to › String › Replace_all_commas_in_a_string.htm
jQuery Data Type How to - Replace all commas in a string
We would like to know how to replace all commas in a string. <!DOCTYPE html> <html> <head> <script type='text/javascript'> <!-- w w w .
🌐
Global Database
globaldatabase.ecpat.org › pdf › book-explore › wp-content › A0I0 › HomePages › javascript-replace-comma-with-newline.pdf
Javascript Replace Comma With Newline
We'll examine several approaches, from simple string manipulation using `replace()` to more sophisticated regular expressions for handling complex scenarios. The simplest way to replace all commas with newlines is using the built-in `replace()` method with a regular expression.