Substituting with regular expressions, in place

Discussion in 'Javascript' started by Sally B., Mar 25, 2005.

  1. Sally B.

    Sally B. Guest

    Hi, how would I go about doing the following: substitute all the
    strings 'the' and 'dog' in the 'mytext' id div only? The following
    doesn't work for 'the'. I also need to use regular expressions for the
    replace command... but I can't get that far. Any reason this doesn't
    work as below:

    <script>
    function start() {
    theText=document.getElementById('mytext');
    theText.replace(/the/g, "xxxx");
    }
    </script>
    </head>
    <body onload="start()">
    <p>the quick brown fox jumps over the lazy dog</p>
    <div id=mytext>the quick brown fox jumps over the lazy dog</div>
    </body>
    </html>


    I have researched a bit and all the 'replace' examples I think should
    work, don't. Also, is this even the best way to do it? If I have a big
    chunk of text in a <div>, what would be a better way to search and
    replace with regular expressions?

    Thanks!
    Sally-B
    Sally B., Mar 25, 2005
    #1
    1. Advertising

  2. Sally B. napisał(a):
    >
    > <script>
    > function start() {
    > theText=document.getElementById('mytext');


    "theText" here refers to the div element, not to its contents, so you
    would need sth like:

    el=document.getElementById('mytext');
    theText=el.innerHTML;


    > theText.replace(/the/g, "xxxx");


    you do not assign the return value. It gets lost. You would need:

    el.innerHTML=theText.replace(/the/g, "xxxx");


    > }
    > </script>



    > <div id=mytext>the quick brown fox jumps over the lazy dog</div>


    <div id="mytext">the quick brown fox jumps over the lazy dog</div>

    Every attribute must have its value quoted



    > </body>
    > </html>
    >



    --
    tomasz cenian tcenian at wa dot home dot pl
    :::: :: : : http://cenian.boo.pl : : :: ::::
    Tomasz Cenian, Mar 25, 2005
    #2
    1. Advertising

  3. Sally B.

    Sally B. Guest

    ahh... thanks. that works. Now, how do I define an array of regular
    expressions and corresponding replacement strings? In php, you could do
    something like:
    $was = {/the/, /dog/};
    $new = {"xxxxx", "cat"};

    etc, then use a preg_replace($was, $new, $string) to replace the
    strings...

    How would I do that in JavaScript?

    Again, is this even the best way to do what I am trying to do: take a
    large block of text and do regex replacements on it?

    Thanks!
    Sally
    Sally B., Mar 25, 2005
    #3
  4. Sally B.

    Mick White Guest

    Sally B. wrote:
    > ahh... thanks. that works. Now, how do I define an array of regular
    > expressions and corresponding replacement strings? In php, you could do
    > something like:
    > $was = {/the/, /dog/};
    > $new = {"xxxxx", "cat"};
    >
    > etc, then use a preg_replace($was, $new, $string) to replace the
    > strings...
    >
    > How would I do that in JavaScript?


    String.replace(regex,replacement)

    "The ink".replace(/[e ]/,"")

    Mick

    >
    > Again, is this even the best way to do what I am trying to do: take a
    > large block of text and do regex replacements on it?
    >
    > Thanks!
    > Sally
    >
    Mick White, Mar 25, 2005
    #4
  5. Sally B.

    Sally B. Guest

    Mick White wrote:
    > Sally B. wrote:
    > > ahh... thanks. that works. Now, how do I define an array of regular
    > > expressions and corresponding replacement strings? In php, you

    could do
    > > something like:
    > > $was = {/the/, /dog/};
    > > $new = {"xxxxx", "cat"};
    > >
    > > etc, then use a preg_replace($was, $new, $string) to replace the
    > > strings...
    > >
    > > How would I do that in JavaScript?

    >
    > String.replace(regex,replacement)
    >
    > "The ink".replace(/[e ]/,"")
    >
    > Mick
    >
    > >


    Is there a way to have, say, 5 different regexs and 5 different
    (corresponding) replacement strings without having 5 separate 'replace'
    commands?

    Thanks
    Sally B., Mar 26, 2005
    #5
  6. Sally B.

    Lee Guest

    Sally B. said:
    >
    >
    >Mick White wrote:
    >> Sally B. wrote:
    >> > ahh... thanks. that works. Now, how do I define an array of regular
    >> > expressions and corresponding replacement strings? In php, you

    >could do
    >> > something like:
    >> > $was = {/the/, /dog/};
    >> > $new = {"xxxxx", "cat"};
    >> >
    >> > etc, then use a preg_replace($was, $new, $string) to replace the
    >> > strings...
    >> >
    >> > How would I do that in JavaScript?

    >>
    >> String.replace(regex,replacement)
    >>
    >> "The ink".replace(/[e ]/,"")
    >>
    >> Mick
    >>
    >> >

    >
    >Is there a way to have, say, 5 different regexs and 5 different
    >(corresponding) replacement strings without having 5 separate 'replace'
    >commands?


    You can put your patterns and replacement texts in arrays and loop through the
    pairs:

    pat = [ /the/, /dog/ ];
    repl = [ "xxxxx", "cat" ];

    for (var i=0;i<pat.length;i++) {
    myString=myString.replace(pat,repl);
    }

    Note that /the/ also matches "other", etc.
    Lee, Mar 26, 2005
    #6
  7. Sally B.

    Sally B. Guest

    Thanks... also, how do I grab the entire body of the document instead
    of just an Id tag. I tried
    "theText=document.getElementByTag('body');"
    to grab the entire body, but no go. Is this because the 'onload' call
    is in the body tag?

    Thanks!
    Sally B., Mar 26, 2005
    #7
  8. Sally B.

    Mick White Guest

    Sally B. wrote:

    > Thanks... also, how do I grab the entire body of the document instead
    > of just an Id tag. I tried
    > "theText=document.getElementByTag('body');"
    > to grab the entire body, but no go. Is this because the 'onload' call
    > is in the body tag?
    >


    theText=document.body.innerHTML;

    Not quite Kosher, but it should do what you want.

    Mick
    Mick White, Mar 26, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jay Douglas
    Replies:
    0
    Views:
    593
    Jay Douglas
    Aug 15, 2003
  2. Andy Fish
    Replies:
    2
    Views:
    393
    Young Seeker
    Dec 29, 2003
  3. Christian Schlichtherle
    Replies:
    8
    Views:
    468
    John Currier
    Jul 5, 2005
  4. John Salerno
    Replies:
    6
    Views:
    331
    John Salerno
    May 10, 2006
  5. Noman Shapiro
    Replies:
    0
    Views:
    220
    Noman Shapiro
    Jul 17, 2013
Loading...

Share This Page