Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #18886 > unrolled thread
| Started by | John English <john.foreign@gmail.com> |
|---|---|
| First post | 2013-03-14 04:56 -0700 |
| Last post | 2013-03-14 17:31 +0100 |
| Articles | 10 — 5 participants |
Back to article view | Back to comp.lang.javascript
RegExp problem John English <john.foreign@gmail.com> - 2013-03-14 04:56 -0700
Re: RegExp problem "Jukka K. Korpela" <jkorpela@cs.tut.fi> - 2013-03-14 14:22 +0200
Re: RegExp problem "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2013-03-14 14:52 +0100
Re: RegExp problem "Jukka K. Korpela" <jkorpela@cs.tut.fi> - 2013-03-14 16:08 +0200
Re: RegExp problem Stefan Weiss <krewecherl@gmail.com> - 2013-03-14 15:25 +0100
Re: RegExp problem Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-03-14 16:47 +0100
Re: RegExp problem Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-03-14 21:11 +0100
Re: RegExp problem "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2013-03-14 19:54 +0100
Re: RegExp problem "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2013-03-14 13:26 +0100
Re: RegExp problem Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-03-14 17:31 +0100
| From | John English <john.foreign@gmail.com> |
|---|---|
| Date | 2013-03-14 04:56 -0700 |
| Subject | RegExp problem |
| Message-ID | <9608f578-525d-4670-9792-0700df15eb41@googlegroups.com> |
I have this string in JavaScript: var s = "</p><ol><li>First\n</li><li>Second\n</li></ol><p>"; Then I do this (to remove the outer "</p>...<p>"): s = s.replace(/^<\/([^> ]+)[^>]*>(.*)<\1>$/,"$2"); Nothing happens (s is unchanged, and if I use match() instead of replace it returns false), but if I go try it at http://www.regular-expressions.info/javascriptexample.html, it works! I've tried all sorts of things (creating a separate regExp object, using //g, taking out the ^$, replacing [^> ]+ with [a-z0-9]*...) but nothing makes any difference. It's driving me nuts. Can anyone tell me what I'm doing wrong here? TIA...
[toc] | [next] | [standalone]
| From | "Jukka K. Korpela" <jkorpela@cs.tut.fi> |
|---|---|
| Date | 2013-03-14 14:22 +0200 |
| Message-ID | <khsfbf$udt$1@dont-email.me> |
| In reply to | #18886 |
2013-03-14 13:56, John English wrote: > I have this string in JavaScript: > > var s = "</p><ol><li>First\n</li><li>Second\n</li></ol><p>"; > > Then I do this (to remove the outer "</p>...<p>"): > > s = s.replace(/^<\/([^> ]+)[^>]*>(.*)<\1>$/,"$2"); > > Nothing happens The period "." does not match newline characters, and the data string contains two newlines (\n). To work around this, replace (.*) by ((.|\n)*) Using regular expressions to process HTML fragments is generally clumsy and fragile. It might be better to operate on the DOM tree rather than serialized HTML. However, I don't quite see the point of this particular operation. Removing the optional </p> tag does not change document structure but may affect rendering due to browser bugs. -- Yucca, http://www.cs.tut.fi/~jkorpela/
[toc] | [prev] | [next] | [standalone]
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
|---|---|
| Date | 2013-03-14 14:52 +0100 |
| Message-ID | <XnsA183976552747eejj99@194.109.133.133> |
| In reply to | #18887 |
Jukka K. Korpela wrote on 14 mrt 2013 in comp.lang.javascript: > The period "." does not match newline characters, and the data string > contains two newlines (\n). To work around this, replace (.*) by > ((.|\n)*) or: [.\n]* or my personal favorite for "any character": [\s\S]* /..../m should work, but it doesn't, I believe. -- Evertjan. The Netherlands. (Please change the x'es to dots in my emailaddress)
[toc] | [prev] | [next] | [standalone]
| From | "Jukka K. Korpela" <jkorpela@cs.tut.fi> |
|---|---|
| Date | 2013-03-14 16:08 +0200 |
| Message-ID | <khslha$1gn$1@dont-email.me> |
| In reply to | #18889 |
2013-03-14 15:52, "Evertjan." wrote: > /..../m should work, but it doesn't, I believe. That's what I though too, but having found out that it does not work, I checked this out and noticed that neither should it work. The effect of "m" flag is just that "^" and "$" match start of line and end of line, respectively, in addition to matching start and end of the string. It does not affect the meaning of ".". -- Yucca, http://www.cs.tut.fi/~jkorpela/
[toc] | [prev] | [next] | [standalone]
| From | Stefan Weiss <krewecherl@gmail.com> |
|---|---|
| Date | 2013-03-14 15:25 +0100 |
| Message-ID | <khsmkn$m11$1@news.albasani.net> |
| In reply to | #18889 |
On 2013-03-14 14:52, Evertjan. wrote: > Jukka K. Korpela wrote on 14 mrt 2013 in comp.lang.javascript: > >> The period "." does not match newline characters, and the data string >> contains two newlines (\n). To work around this, replace (.*) by >> ((.|\n)*) > > or: > > [.\n]* That's a literal dot character. It does nothing special inside a character class. > or my personal favorite for "any character": > > [\s\S]* Shortest way to match "any character including \n": [^] - stefan
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2013-03-14 16:47 +0100 |
| Message-ID | <1514068.yciYkkSlex@PointedEars.de> |
| In reply to | #18891 |
Stefan Weiss wrote: > On 2013-03-14 14:52, Evertjan. wrote: >> or my personal favorite for "any character": >> >> [\s\S]* > > Shortest way to match "any character including \n": > [^] My research showed that this is a syntax error in Microsoft JScript in version 5.8.23141 (IE/MSHTML 8.0) and before (and probably in version 9.0.16440 in Browser Compatibility Mode, the same as with trailing commas in Object and Array initialisers), and in Opera ECMAScript in version 9.64 and before. [\s\S] is the safe approach. -- PointedEars Twitter: @PointedEars2 Please do not Cc: me. / Bitte keine Kopien per E-Mail.
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2013-03-14 21:11 +0100 |
| Message-ID | <3460713.alAdkCt8v1@PointedEars.de> |
| In reply to | #18892 |
Thomas 'PointedEars' Lahn wrote:
> Stefan Weiss wrote:
>> On 2013-03-14 14:52, Evertjan. wrote:
>>> or my personal favorite for "any character":
>>>
>>> [\s\S]*
>>
>> Shortest way to match "any character including \n":
>> [^]
>
> My research showed that this is a syntax error in Microsoft JScript in
> version 5.8.23141 (IE/MSHTML 8.0) and before (and probably in version
> 9.0.16440 in Browser Compatibility Mode, the same as with trailing commas
> in Object and Array initialisers), and in Opera ECMAScript in version 9.64
> and before. [\s\S] is the safe approach.
However, it should be noted that JSX:regexp.js [1, 2] supports PCRE_DOTALL
since revision 275 (2012-09-19 CE) with
var rx = new jsx.regexp.RegExp(/./, "s");
or
var rx = new jsx.regexp.RegExp(".", "s");
being equivalent to
var rx = /[\S\s]/;
The pattern-match modifiers “(?s)” and “(?-s)" are also supported with a
first String argument. (But they apply to the rest of the expression, not
only the current group, as of yet.)
This is particularly useful if you have many any-character occurrences in
the expression. The “.” will _not_ be expanded in a character class, so as
to retain its original meaning:
var rx = new jsx.regexp.RegExp(/.[.]./, "s");
is equivalent to
var rx = /[\S\s][.][\S\s]/;
jsx.regexp.RegExp() is a factory/constructor that returns a reference to an
augmented RegExp instance. So from here everything in your script can stay
the same (global, case-insensitive, multi-line, and other flags are
supported by the function as well). You may carefully import it into the
global namespace, like
jsx._import(jsx.regexp, ["RegExp"], null, ["RegExp2"]); // [3]
or a variable declaration if you want to keep it efficient but local.
________
[1] <http://PointedEars.de/scripts/test/regexp>
[2]
<http://pointedears.de/websvn/filedetails.php?repname=JSX&path=%2Ftrunk%2Fregexp.js>
[3]
<http://pointedears.de/websvn/filedetails.php?repname=JSX&path=%2Ftrunk%2Fobject.js>
--
PointedEars
Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.
[toc] | [prev] | [next] | [standalone]
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
|---|---|
| Date | 2013-03-14 19:54 +0100 |
| Message-ID | <XnsA183CA95CBECEeejj99@194.109.133.133> |
| In reply to | #18891 |
Stefan Weiss wrote on 14 mrt 2013 in comp.lang.javascript: > On 2013-03-14 14:52, Evertjan. wrote: >> Jukka K. Korpela wrote on 14 mrt 2013 in comp.lang.javascript: >> >>> The period "." does not match newline characters, and the data string >>> contains two newlines (\n). To work around this, replace (.*) by >>> ((.|\n)*) >> >> or: >> >> [.\n]* right, sorry. > That's a literal dot character. > It does nothing special inside a character class. > >> or my personal favorite for "any character": >> >> [\s\S]* > > Shortest way to match "any character including \n": > [^] > > > - stefan > -- Evertjan. The Netherlands. (Please change the x'es to dots in my emailaddress)
[toc] | [prev] | [next] | [standalone]
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
|---|---|
| Date | 2013-03-14 13:26 +0100 |
| Message-ID | <XnsA18388C9BF1D6eejj99@194.109.133.133> |
| In reply to | #18886 |
John English wrote on 14 mrt 2013 in comp.lang.javascript: > I have this string in JavaScript: > > var s = "</p><ol><li>First\n</li><li>Second\n</li></ol><p>"; A javascript variable containing a html string. > > Then I do this (to remove the outer "</p>...<p>"): > > s = s.replace(/^<\/([^> ]+)[^>]*>(.*)<\1>$/,"$2"); s = s.replace(/^<\/p>|<p>$/g,''); [..] > It's driving me nuts. Can anyone tell me what I'm doing wrong here? Never drive your nuts, javascript is not golf, PE should agree. -- Evertjan. The Netherlands. (Please change the x'es to dots in my emailaddress)
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2013-03-14 17:31 +0100 |
| Message-ID | <2116422.PPeRs11RAM@PointedEars.de> |
| In reply to | #18886 |
John English wrote:
> I have this string in JavaScript:
>
> var s = "</p><ol><li>First\n</li><li>Second\n</li></ol><p>";
>
> Then I do this (to remove the outer "</p>...<p>"):
One wonders how it got there in the first place, since it is not Valid.
One also wonders why you think you need to (or could, trivially) fix that.
> s = s.replace(/^<\/([^> ]+)[^>]*>(.*)<\1>$/,"$2");
<strong title="You did not expect that x > y, did you?"
style="color: red; font-weight: bold; text-transform: uppercase"
>wrong</strong>
This expression does _not_ accurately match HTML tags or elements, or XML
tags or elements for that matter. See HTML5 (CR) and XHTML 1.0/1.1 (REC)
for details.
> Nothing happens (s is unchanged, and if I use match() instead of replace
> it returns false), but if I go try it at
> http://www.regular-expressions.info/javascriptexample.html, it works!
>
> I've tried all sorts of things (creating a separate regExp object, using
> //g, taking out the ^$, replacing [^> ]+ with [a-z0-9]*...) but nothing
> makes any difference.
>
> It's driving me nuts. Can anyone tell me what I'm doing wrong here?
You are using “.” which does not match "\n", so that expression matches as
far as
</p><ol><li>First\n</li><li>Second\n</li></ol><p>
`---'`-----------'
: :
: '-------.
: :
,----------------.,--.
^<\/([^> ]+)[^>]*>(.*)<\1>$
(use a fixed-width font). Then it fails, because "\n" is not matched by
“<”.
That said, like so many other people before you, you are trying to apply an
expression describing a Chomsky type-3 language to match a sentence of a
text in a Chomsky type-2 language, which usually *must* fail.
--
PointedEars
Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.javascript
csiph-web