Skip to content Skip to sidebar Skip to footer

Javascript To Find And Replace Text

I'm making an extension for google chrome and i would like to be able find certain pieces of text and replace them with random letters or numbers using javascript except the certai

Solution 1:

Try using the .replace() method of JavaScript. Supposing you have a div like so for containing text: <div id="test">Original Text</div>, use this JavaScript code:

var orignalstring = document.getElementById("test").innerHTML;
var newstring = orignalstring.replace("original","replaced");
document.getElementById("test").innerHTML = newstring;

Basically, this will identify the entire content of the whole div, then find certain text and replace those terms, like you asked. If you want to replace multiple strings in one command, try this: How to replace multiple strings with the .replace() Method?. This is a question I asked a few weeks back about .replace().

Also, try this JSFiddle: http://jsfiddle.net/tGMaN/

If you want the user to be able to define the text to replace and the replacement text, you can easily do this through text fields or a prompt box, and these values can be stored as variables which are called in the .replace() method.

Post a Comment for "Javascript To Find And Replace Text"