Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > relace() with string variable as part of regular expression

Reply
Thread Tools

relace() with string variable as part of regular expression

 
 
simplicity
Guest
Posts: n/a
 
      12-11-2007
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.
 
Reply With Quote
 
 
 
 
RobG
Guest
Posts: n/a
 
      12-12-2007
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
 
Reply With Quote
 
 
 
 
Dr J R Stockton
Guest
Posts: n/a
 
      12-12-2007
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Replacing part of a matched regular expression using gsub Ben Ruby 4 03-25-2008 10:27 AM
Regular Expression to match the domain part of an email address emzyme20@hotmail.com Java 9 12-02-2006 09:59 AM
Set a variable from a substring of another variable using Regular Expression Tony Perl Misc 2 04-20-2005 11:57 PM
noob question: Trying to extract part of a string in a variable to another variable cayenne Perl Misc 19 05-19-2004 11:22 PM
Dynamically changing the regular expression of Regular Expression validator VSK ASP .Net 2 08-24-2003 02:47 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57