Search code snippets, questions, articles...

Replace characters globally Javascript

//Case sensitive
var str = "Color is Green and green is a cold color";
var result = str.replace(/green/g, "blue");
//output - Color is Green and blue is a cold color

//Applly for case insensitive
var res = str.replace(/green/gi, "blue");
//output - Color is blue and blue is a cold colors
Best JSON Validator, JSON Tree Viewer, JSON Beautifier at same place.

You can use .replace() function to replace characters with some other characters in javascript. Use 'g' with the string that needs to be replaced for case sensitive replacement and 'gi' for case insensitive replacement.

Was this helpful?