relace() with string variable as part of regular expression

Discussion in 'Javascript' started by simplicity, Dec 11, 2007.

  1. simplicity

    simplicity Guest

    This may be a basic thing but I cannot find it in any tutorial.

    I want to have the mechanism to replace any arbitrary string by
    highlighted one in a large string - basically a search feature in the
    HTML code.

    I get the search string from the textbox:

    var searchString= document.getElementById("searchTxt").value

    The search is performed on the text in the <div>

    var unfilteredData = document.getElementById("container").innerHTML

    I want to
    (1) scan unfilteredData for all occurences of searchString,
    (2) replace them with <span class="highlight"> + searchString + </
    span>
    (3) put them back into "container", so all occurences of searchString
    are highlighted.

    When I try

    unfilteredData.replace(searchString, "<span class=\"highlight\">" +
    searchString + "</span>"

    I get the first occurence only - as expected.

    My question is how can I use current value of searchString as part of
    regular expression /.../g? I tried
    "/" + searchString + "/g" with no effect.

    I will appreciate any help.
     
    simplicity, Dec 11, 2007
    #1
    1. Advertising

  2. simplicity

    RobG Guest

    simplicity wrote:
    > This may be a basic thing but I cannot find it in any tutorial.
    >
    > I want to have the mechanism to replace any arbitrary string by
    > highlighted one in a large string - basically a search feature in the
    > HTML code.
    >
    > I get the search string from the textbox:
    >
    > var searchString= document.getElementById("searchTxt").value
    >
    > The search is performed on the text in the <div>
    >
    > var unfilteredData = document.getElementById("container").innerHTML
    >
    > I want to
    > (1) scan unfilteredData for all occurences of searchString,
    > (2) replace them with <span class="highlight"> + searchString + </
    > span>
    > (3) put them back into "container", so all occurences of searchString
    > are highlighted.
    >
    > When I try
    >
    > unfilteredData.replace(searchString, "<span class=\"highlight\">" +


    I would use single quotes inside doubles:

    ..."<span class='highlight'>" +


    In this particular case, for HTML, the inner quotes around highlight
    aren't needed.


    > searchString + "</span>"
    >
    > I get the first occurence only - as expected.
    >
    > My question is how can I use current value of searchString as part of
    > regular expression /.../g? I tried
    > "/" + searchString + "/g" with no effect.


    Use something like:

    var re = new RegExp(searchString, 'g');
    var newString = unfilteredData.replace(re, ...);



    --
    Rob
    "We shall not cease from exploration, and the end of all our
    exploring will be to arrive where we started and know the
    place for the first time." -- T. S. Eliot
     
    RobG, Dec 12, 2007
    #2
    1. Advertising

  3. In comp.lang.javascript message <f098dbac-c1f4-4abb-a982-2b610a5f863c@d2
    1g2000prf.googlegroups.com>, Tue, 11 Dec 2007 11:01:57, simplicity
    <> posted:
    >
    >My question is how can I use current value of searchString as part of
    >regular expression /.../g? I tried
    >"/" + searchString + "/g" with no effect.


    var RE = new RegExp(S1 + searchString + S2, S3) // S1 S2 S3 strings.

    Seek "A literal text format or the RE constructor function." after
    replacing RE by RegExp (changed to prevent that seek finding, in
    perpetuity, this article).

    It's a good idea to read the newsgroup c.l.j and its FAQ. See below.

    --
    (c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
    news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
     
    Dr J R Stockton, Dec 12, 2007
    #3
    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. VSK
    Replies:
    2
    Views:
    2,310
  2. Replies:
    9
    Views:
    2,430
    Jussi Piitulainen
    Dec 2, 2006
  3. Jack
    Replies:
    8
    Views:
    288
  4. Ben
    Replies:
    4
    Views:
    138
    Robert Klemme
    Mar 25, 2008
  5. Tony
    Replies:
    2
    Views:
    129
    Tad McClellan
    Apr 21, 2005
Loading...

Share This Page