Replace all words on a page

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • gregpinero@gmail.com

    Replace all words on a page

    I'm trying to write a little script that will have a list of word pairs
    which will loop through that list and replace all instances of each
    word with the other word.

    I'm very new to javascript so I'm not quite sure where to get started.
    Here is my python "prototype" in case you want a better idea of what
    I'm trying to do:

    <code>
    corpus="""
    We always love war. Run away all of you he ordered.
    """ #Just some random text I made up. This would be the html in the
    javascript version

    findreplacepair s=[
    ("war","peace") ,
    ("run","walk "),
    ("order","reque st")
    ]

    for item in findreplacepair s:
    \t corpus.replace( item[0],item[1]) #replaces all instances

    </code>

    Thanks,

    Greg

  • McKirahan

    #2
    Re: Replace all words on a page

    <gregpinero@gma il.com> wrote in message
    news:1123701237 .852476.202380@ f14g2000cwb.goo glegroups.com.. .[color=blue]
    > I'm trying to write a little script that will have a list of word pairs
    > which will loop through that list and replace all instances of each
    > word with the other word.
    >
    > I'm very new to javascript so I'm not quite sure where to get started.
    > Here is my python "prototype" in case you want a better idea of what
    > I'm trying to do:
    >
    > <code>
    > corpus="""
    > We always love war. Run away all of you he ordered.
    > """ #Just some random text I made up. This would be the html in the
    > javascript version
    >
    > findreplacepair s=[
    > ("war","peace") ,
    > ("run","walk "),
    > ("order","reque st")
    > ]
    >
    > for item in findreplacepair s:
    > \t corpus.replace( item[0],item[1]) #replaces all instances
    >
    > </code>
    >
    > Thanks,
    >
    > Greg
    >[/color]

    Will this get you started? Watch for word-wrap.

    It ignores capitalization and replaces embedded words.

    Before:
    We always love war. Run away all of you he ordered.
    After:
    We always love peace. walk away all of you he requested.


    <script type="text/javascript">
    var corpus = "We always love war. Run away all of you he ordered."
    var pairs = [
    "war=peace" ,
    "run=walk",
    "order=requ est"];
    var temper = corpus;
    for (var i=0; i<pairs.length ; i++) {
    var words = pairs[i].split("=");
    var regex = new RegExp(words[0],"ig");
    temper = temper.replace( regex,words[1]);
    }
    alert(corpus + "\n" + temper);
    </script>


    Comment

    • gregpinero@gmail.com

      #3
      Re: Replace all words on a page

      very neat! Thanks! My main remaining question is how do I get at all
      the text from a page? I need some sort of dom thingy from what I've
      read.
      A few other questions below:

      <script type="text/javascript">
      var corpus = "We always love war. Run away all of you he ordered."
      var pairs = [
      "war=peace" ,
      "run=walk",
      "order=requ est"];
      var temper = corpus;
      for (var i=0; i<pairs.length ; i++) {
      var words = pairs[i].split("=");
      var regex = new RegExp(words[0],"ig");
      [GP] What does above line do?
      temper = temper.replace( regex,words[1]);
      [GP] in above line, how do you say replace all instances vs replace
      first?
      }

      alert(corpus + "\n" + temper);
      </script>

      Thanks again,

      Greg

      Comment

      • McKirahan

        #4
        Re: Replace all words on a page

        <gregpinero@gma il.com> wrote in message
        news:1123708788 .840515.168590@ f14g2000cwb.goo glegroups.com.. .[color=blue]
        > very neat! Thanks! My main remaining question is how do I get at all
        > the text from a page? I need some sort of dom thingy from what I've
        > read.
        > A few other questions below:
        >
        > <script type="text/javascript">
        > var corpus = "We always love war. Run away all of you he ordered."
        > var pairs = [
        > "war=peace" ,
        > "run=walk",
        > "order=requ est"];
        > var temper = corpus;
        > for (var i=0; i<pairs.length ; i++) {
        > var words = pairs[i].split("=");
        > var regex = new RegExp(words[0],"ig");
        > [GP] What does above line do?
        > temper = temper.replace( regex,words[1]);
        > [GP] in above line, how do you say replace all instances vs replace
        > first?
        > }
        >
        > alert(corpus + "\n" + temper);
        > </script>
        >
        > Thanks again,
        >
        > Greg
        >[/color]

        "how do I get at all the text from a page?"
        you can use XMLHTTP to retrieve a Web page;
        is that what you mean by "from a page"?


        var regex = new RegExp(words[0],"ig");
        [GP] What does above line do?

        Constructs a regular expression of the "replace with" word'
        "ig" specifies ignore case and global (all instances).

        temper = temper.replace( regex,words[1]);
        [GP] in above line, how do you say replace all instances vs replace first?

        The "g" in the "ig" says replace all instances.
        Remove it if you don't want to.


        Comment

        Working...