Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #26819 > unrolled thread

Can RegExp() do this??

Started bydean@hannotte.com
First post2015-02-08 05:57 -0800
Last post2015-02-10 17:04 +0100
Articles 4 — 3 participants

Back to article view | Back to comp.lang.javascript


Contents

  Can RegExp() do this?? dean@hannotte.com - 2015-02-08 05:57 -0800
    Re: Can RegExp() do this?? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-02-08 15:17 +0100
      Re: Can RegExp() do this?? Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2015-02-09 15:56 -0200
        Re: Can RegExp() do this?? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-02-10 17:04 +0100

#26819 — Can RegExp() do this??

Fromdean@hannotte.com
Date2015-02-08 05:57 -0800
SubjectCan RegExp() do this??
Message-ID<a81c57d8-c415-41dd-87b6-bd19b5697590@googlegroups.com>
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?

[toc] | [next] | [standalone]


#26820

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2015-02-08 15:17 +0100
Message-ID<5013331.C6z0DViB7m@PointedEars.de>
In reply to#26819
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/855543>
<http://en.wikipedia.org/wiki/Chomsky_hierarchy>

> 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.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not cc me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [next] | [standalone]


#26828

FromJoao Rodrigues <groups_jr-1@yahoo.com.br>
Date2015-02-09 15:56 -0200
Message-ID<mbasd3$t2t$1@speranza.aioe.org>
In reply to#26820
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

[toc] | [prev] | [next] | [standalone]


#26835

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2015-02-10 17:04 +0100
Message-ID<4359125.6fUziKdlIv@PointedEars.de>
In reply to#26828
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.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not cc me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web