Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #18893
| Message-ID | <2116422.PPeRs11RAM@PointedEars.de> (permalink) |
|---|---|
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
| Organization | PointedEars Software (PES) |
| Date | 2013-03-14 17:31 +0100 |
| Subject | Re: RegExp problem |
| Newsgroups | comp.lang.javascript |
| References | <9608f578-525d-4670-9792-0700df15eb41@googlegroups.com> |
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.
Back to comp.lang.javascript | Previous | Next — Previous in thread | Find similar | Unroll thread
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
csiph-web