I want to "extract" a text from within another arbitrary (and larger) html text, ie wrap it in tags so that I can play with it using javascript.

By and large, this is just an extended text-search, ie "find me 2 'the's, 1 'fox', 1 'jumped' ..."

Three questions, reverend brothers:

  • What is the most efficient way of doing this?
  • The desired text will use the same word more than once. How do I avoid tagging the same instance of the word twice, and only tag as many instances as I desire?
  • And lastly, if a word can't be found, I want to make it from letters grabbed from other words. What's the best way of making sure I don't grab letters from the words I've previously grabbed?

    I was kind of hoping for a really gnarly, single regexp to do this, but as this is for a CGI script it needs above all to be fast.

    muchos gracias


    In reply to Highlighting a text inside another one. by TomSW

    Title:
    Use:  <p> text here (a paragraph) </p>
    and:  <code> code here </code>
    to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.