Try:
$(function(){
   var keywords = [ ... ];
   $.each(keywords, function(){
      var kw = this, rx = new RegExp('/^ '+kw+' $/');
      $(".keyword:contains('"+kw+"')") .each(function(){
            $(this).text($(this).text().replace(rx, '<span>'+kw+'</
span>'));
      });
   });
});

It's untested, but I think that should work? The Regex is adding
whitespace to either side of the keyword intentionally, to only catch
full words (so " the RED fox" gets matched but " was near a REDwood
tree." does not. Though, I'm not a regexpert, so I'm not positive what
I've given you is the right way to go there, but hopefully it gives
you a base to work from.

On Feb 3, 10:20 am, paulinstl <paulsha...@gmail.com> wrote:
> Rob,
>
> My problem is that I have no way of knowing where the keyword may
> appear.  It might be tabular data (<TD>) or in descriptive paragraph
> (<p>).
>
> So if my keyword is RED, then...
>
> BEFORE
> <p> the red fox </p>
>
> AFTER
> <p>the <span>red</span> fox</p>
>
> I'll have an array of keywords as well. var arrayx["one", "two"
> "three"]
>
> On Feb 2, 5:57 pm, RobG <rg...@iinet.net.au> wrote:
>
> > On Feb 3, 8:01 am, paulinstl <paulsha...@gmail.com> wrote:
>
> > > I'm looking for a way to locate keywords to help the end user out.
>
> > > For instance, if I want to find the word "polar" then i'd like my
> > > function to locate it, wrap it with a span, and assign it a function.
>
> > > so far I can locate using a content filter and classname like this...
>
> > > $(".keyword:contains('polar')")
>
> > > But this selects the whole element (paragraph, div, whatever)
>
> > > how do i focus in on JUST the text?
>
> > The most reliable method is to traverse the part of the DOM you want
> > to search and look at the value of text nodes.  If you want to
> > highlight a particular character pattern, use a regular expression and
> > DOM methods to wrap it in a suitable element, say a span.  It would
> > also be prudent to also put adjacent text in spans too, e.g.
>
> >   <div>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</dv>
>
> > might become:
>
> >   <div><span>Lorem ipsum </span><span class="foo">dolor</span>
> >   <span> sit amet, consectetur adipiscing elit.</span></dv>
>
> > That way you can also deal with text found inside elements like B or
> > QUOTE if necessary.
>
> > Other approaches such as using a regular exprssion and an element's
> > innerHTML property, are likely to be error prone.
>
> > --
> > Rob

Reply via email to