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