relace() with string variable as part of regular expression

S

simplicity

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.
 
R

RobG

simplicity said:
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, ...);
 
D

Dr J R Stockton

In comp.lang.javascript message <f098dbac-c1f4-4abb-a982-2b610a5f863c@d2
1g2000prf.googlegroups.com>, Tue, 11 Dec 2007 11:01:57, simplicity
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top