google chrome - Javascript to find and replace text - Stack Overflow

I'm making an extension for google chrome and i would like to be able find certain pieces of 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 certain text is a variable entered by the user for example they enter the word dog and all words that say dog are found and replaced as explained before . HELP greatly wanted

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 certain text is a variable entered by the user for example they enter the word dog and all words that say dog are found and replaced as explained before . HELP greatly wanted

Share Improve this question asked Jan 5, 2013 at 0:56 ObcureObcure 1,0014 gold badges12 silver badges24 bronze badges 2
  • 1 Where do you plan to search for the "certain pieces of text"? The entire page? In certain form elements? You need to make your question more specific. Please include examples. – jahroy Commented Jan 5, 2013 at 1:01
  • 1 What have you tried? Did you try entering 'javascript replace text' in any search engine? – Stuart Commented Jan 5, 2013 at 1:44
Add a ment  | 

2 Answers 2

Reset to default 3

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 mand, 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/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.

The String object has lots of useful methods.

var theNewString = theString.replace(findThis, replaceWithThis);

https://developer.mozilla/en-US/docs/JavaScript/Reference/Global_Objects/String/replace

// Generates a string of random letters
function generateRandomKey(number) {
    var key = String();
    var values = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    for(var i = 0; i < number; i++) {
        key += values.charAt(Math.floor(Math.random() * values.length));
    }
    return key;
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744236204a4564481.html

相关推荐

  • google chrome - Javascript to find and replace text - Stack Overflow

    I'm making an extension for google chrome and i would like to be able find certain pieces of text

    8天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信