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 OverflowThe 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
Just for fun:
var mystring = "this,is,a,test"
var newchar = '|'
mystring = mystring.split(',').join(newchar);
Replacing space and comma from a string using regex
use replace() to remove all commas from string
lightning aura components - Remove multiple commas between two strings - Salesforce Stack Exchange
Remove multiple commas at end of string?
That's basic JavaScript. It doesn't really even matter how many commas are there, 2 or a million. Just replace them all with a single instance:
cptCode = cptCode.replace(/,+/g,',');
Where /,+/g is a Regular Expression meaning "find a comma (,) followed by any further commas (+), globally within the string (/.../g)." Each match will then be "replaced" with just a single comma (the second parameter).
Simple solution:
Add them to a list, then join the list.
List<String> values = <field>.split( ',' );
String result = String.join( values, ',' );
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?
Yes, there is. Use the replace function with a regex instead. That has a few advantages. Firstly, you don't have to call it twice anymore. Secondly it's really easy to account for an arbitrary amount of spaces and an optional comma:
aString = aString.replace(/[ ]*,[ ]*|[ ]+/g, '*');
Note that the square brackets around the spaces are optional, but I find they make the space characters more easily readable. If you want to allow/remove any kind of whitespace there (tabs and line breaks, too), use \s instead:
aString = aString.replace(/\s*,\s*|\s+,/g, '*');
Note that in both cases we cannot simply make the comma optional, because that would allow zero-width matches, which would introduce a * at every single position in the string. (Thanks to CruorVult for pointing this out)
If you want to replace all non-word characters try this:
str.replace(/\W+/g, '*');