JavaScript Replace Text

April 26, 2020
Written By Bobby

Exploring how creativity, culture, and technology connect us.

If you ever get into a situation where a string is received with some text that either shouldn’t be there, or is in there incorrectly, here’s a quick solution for that, use String.replace():

var text = "What the for an example";

alert("Before: " + text);


if (text && text.includes("What the")){
	text = text.replace("What the","Text");
}

alert("After: " + text);

Leave a Comment