JavaScript Regex Exclude + Include pattern match -


i using javascript regexp search highlighting on html content.

to using:

data.replace( new regexp("("+search+")", 'g'), "<b id='searchhighlight'>$1</b>" ); 

where data whole of html content , search search string.

when searching for, e.g., h, highlight h in words (the, there, etc...) along instances in tags "<h1 id="title"> </h1>", etc.

i can't go alternative approach since need highlight same html content same style.

i have read solutions like:

var input = "a dog <span class='something'> had  </span> , cat"; // remove tag-like var temp = input.replace(/<.+?>/g, ""); // perform search var matches = new regexp(exp, "g").exec(temp); 

but since need highlight search text in same html content, can't strip out existing tags. there way include , exclude search in regexp, could, example, highlight h in "the" "t<b id='searchhighlight'>h</b>e"
, not allow "<h1 id="title">test</h1>" corrupted thus: "<<b id='searchhighlight'>h</b>1 id="title">test</<b id='searchhighlight'>h</b>1>"?

the html content static , looks this:

    <h1 id="title">samples</h1>         <div id="content">             <div  class="principle">         <h2 id="heading">                        principle</h2>           <p>             fda recognizes samples important part of ensuring right drugs provided right patients. under prescription drug marketing act (pdma), sales representative permitted provide prescription drug samples eligible healthcare professionals (hcps). in order bms provide service, representatives must strictly abide applicable compliance standards pertaining distribution of samples.</p></div> <h2 id="heading">                        why matter?</h2>         <p>             office of inspector general (oig) recognizes samples can have monetary value hcps and, when used improperly, may have implications under federal false claims act , federal anti-kickback act. minimize risk of such liability, oig requires clear , conspicuous labeling of individual samples units cannot sold.&nbsp; bms , business partners label every sample package meet requirement.&nbsp; additionally, hcp signature statement acknowledges samples not sold, billed or provided family members or friends.</p>         <h2 id="heading">              smart partner?</h2>         <p>             smart acronym &ldquo;samples management , representatives together&rdquo;.&nbsp; smart partner has thorough understanding of bms sample requirements , available assist field day-to-day policy or procedure questions related sample activity. smart partner also:</p>          <ul>             <li style="margin-left:22pt;"> monitor adherence bms&rsquo;s sample requirements.</li>             <li style="margin-left:22pt;"> act conduit sharing sample compliance issues , best practices.</li>             <li style="margin-left:22pt;"> respond day-to-day sample accountability questions within 2 business days of receipt.</li>         </ul>         <p>              smart partner can reached @ 888-475-2328, option 3.</p>         <h2 id="heading">              bms sample accountability policies &amp; procedures</h2>         <p>             responsibility of each sales representative read, understand , follow bms field sample accountability procedures, uspsm-sop-101. basic expectations are:</p>         <ul>             <li style="margin-left:22pt;"> transmit sample activity communicating tablet host server on <strong>daily</strong> basis.</li>             <li style="margin-left:22pt;"> maintain 4 6 week inventory of samples rather excessive, larger inventories more difficult manage , increase risk of non-compliance.</li>             <li style="margin-left:22pt;"> witness hcp&rsquo;s signatures confirm request , receipt of samples.</li>         </ul> </div> 

the contents scattered , not in 1 tag. dom manipulation not solution me.

if can sure there no < or > in tag's attributes, use

data = data.replace(      new regexp( "(" + search + "(?![^<>]*>))", 'g' ),         "<b id='searchhighlight'>$1</b>" ); 

the negative look-ahead (?![^<>]*>) prevents replacement if > appears before < ahead in string, if inside tag.

this far fool-proof, may enough.

btw, matching globally, i.e. making more 1 replacement, id='searchhighlight' should class='searchhighlight'.

and need careful search not contain regex special characters.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -