kelvin
0
Q:

replacing characters in string javascript

var str = "JavaScript replace method test";
var res = str.replace("test", "success");  
//res = Javscript replace method success
17
var myStr = 'this,is,a,test';
var newStr = myStr.replace(/,/g, '-');

console.log( newStr );  // "this-is-a-test"
1
let string = 'soandso, my name is soandso';

let replaced = string.replace(/soandso/gi, 'Dylan');

console.log(replaced); //Dylan, my name is Dylan
9
str.replace(/hello/g, 'hi');
// the g is to show it's a global change, not one-time change.

str.replace(new RegExp('hello', 'g'), 'hi');
1

New to Communities?

Join the community