Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regular Expressions in Internet Explorer 5.0

Status
Not open for further replies.

mlundin617

Technical User
Feb 16, 2001
4
0
0
US
I use the following function to allow a search to highlight the text it finds on a document:

function replaceText ( node ) {
searcher = new RegExp ( textString, "gi" );
firsthit = searcher.exec(node.nodeValue)
if ( firsthit ) {
node.parentNode.innerHTML = node.parentNode.innerHTML.replace( searcher, &quot;<span style='background:#ffff33' >&quot; + RegExp.lastMatch +&quot;</span>&quot;);
}
}

The issue I have is with the RegExp.lastMatch. The function that I expect from it is to replace the text with the text that it found, including the case. So, if you did a search for &quot;Good&quot; and it found &quot;good&quot;, the text would be replaced with &quot;good&quot; (lowercase c).

This works wonderfully in Internet Explorer 5.5, but when I try it in 5.0 a replacement takes place, but instead of the last match, &quot;undefined&quot; is placed within the span.

I've tried other RegExp methods as well, and the nearest I can get in 5.0 is by using the first hit's case value, with:

firsthit(0) instead of RegExp.lastMatch

With this method I get the word, and most of the time the case is correct, but not if it's found multiple times in the same node, then the case reflects the first hit in that node.

Does anyone out there know a way to make RegExp.lastMatch work in Internet Explorer 5.0, or have a replacement function that would do the same thing.

Any help would be greatly appreciated. Thanks much,

Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top