[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.javascript

Can RegExp() do this??

dean

2/8/2015 1:57:00 PM

I want to highlight in a string of HTML a search term that the user specifies, perhaps by using <b> and </b> elements. This is effortless in Javascript except for the fact that the HTML may contain complicated elements that SHOULDN'T be highlighted. I want to skip over ANY islands of HTML elements -- which might include length <script></script> islands and even meta-element islands of my own design which begin with "<<" and end with ">>".

How can I highlight all cases of "this", for example, without unintentionally highlighting cases of "this" that occur within islands of HTML and META-HTML. To be more specific, I want to use RegExp() to write a function I could call as follows:

highlit_html = highlight_html("this", "<b>", "</b>", "<", ">");

where the first operand specifies the string to highlight, the second and third operands specify how to highlight the string, and the fourth and fifth operands specify islands of text to skip over.

IS THIS POSSIBLE?
3 Answers

Thomas 'PointedEars' Lahn

2/8/2015 2:17:00 PM

0

dean@hannotte.com wrote:
^^^^^^^^^^^^^^^^^
Your real name belongs there.

> How can I highlight all cases of "this", for example, without
> unintentionally highlighting cases of "this" that occur within islands of
> HTML and META-HTML.

Is that a question?
^

You can use String.prototype.replace() passing a reference to a RegExp
instance with alternation and global flag set while giving precedence to the
alternatives that match substrings that you wish to ignore, and a reference
to a Function instance for replacer function. Then return from that
function the match for them, and the highlighted match for the rest.

That said, at your current position in the learning curve you should not
attempt to parse provably non-regular languages like HTML using regular
expressions.

<http://stackoverflow.com/a/1732454/...
<http://en.wikipedia.org/wiki/Chomsky_hie...

> IS THIS POSSIBLE?

Yes. Please do not shout. And do not use the borked, spammed, and
troll-infested Google Groups for posting; use a decent newsreader.

--
PointedEars
FAQ: <http://PointedEars.... | SVN: <http://PointedEars.de...
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-...
Please do not cc me. / Bitte keine Kopien per E-Mail.

JR

2/9/2015 5:57:00 PM

0

Thomas 'PointedEars' Lahn wrote:

> You can use String.prototype.replace() passing a reference to a RegExp
> instance with alternation and global flag set while giving precedence to the
> alternatives that match substrings that you wish to ignore, and a reference
> to a Function instance for replacer function. Then return from that
> function the match for them, and the highlighted match for the rest.
^^^^^ ^^^^^

>
> That said, at your current position in the learning curve you should not
> attempt to parse provably non-regular languages like HTML using regular
^^^^^

The OP doesn't seem to want parsing the HTML document here.


--
Joao Rodrigues

Thomas 'PointedEars' Lahn

2/10/2015 4:04:00 PM

0

Joao Rodrigues wrote:

> Thomas 'PointedEars' Lahn wrote:
>> You can use String.prototype.replace() passing a reference to a RegExp
>> instance with alternation and global flag set while giving precedence to
>> the alternatives that match substrings that you wish to ignore, and a
>> reference to a Function instance for replacer function. Then return from
>> that function the match for them, and the highlighted match for the rest.
> ^^^^^ ^^^^^
>> That said, at your current position in the learning curve you should not
>> attempt to parse provably non-regular languages like HTML using regular
> ^^^^^
>
> The OP doesn't seem to want parsing the HTML document here.

For what they want they need to be able to tell apart matches within
â??scriptâ? elements and â??meta-element islandsâ? (as they call it; the resulting
markup is probably invalid) from the other matches that they are interested
in. That means parsing HTML (I did not say they would need to parse the
entire document). Are you confusing parsing with rendering?

HTH

--
PointedEars
FAQ: <http://PointedEars.... | SVN: <http://PointedEars.de...
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-...
Please do not cc me. / Bitte keine Kopien per E-Mail.