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


Groups > comp.lang.javascript > #18893

Re: RegExp problem

Path csiph.com!usenet.pasdenom.info!news.albasani.net!news.teledata-fn.de!newsfeed.arcor.de!newsspool3.arcor-online.net!news.arcor.de.POSTED!not-for-mail
Content-Type text/plain; charset="UTF-8"
Message-ID <2116422.PPeRs11RAM@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Reply-To Thomas 'PointedEars' Lahn <usenet@PointedEars.de>
Organization PointedEars Software (PES)
Date Thu, 14 Mar 2013 17:31:30 +0100
User-Agent KNode/4.8.5
Content-Transfer-Encoding 8Bit
X-Face %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&.<b';Md8`dH6iqhT)6C^.Px|[=M@7=Ik[_w<%n1Up"LPQNu2m8|L!/3iby{-]A+#YE}Kl{Cw$\U!kD%K}\2jz"QQP6Uqr],./"?;=4v
Face iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAGFBMVEXTxa4RFk5dUWANED8PFEfy7+MGBiW+n3ZNF/QuAAACaElEQVQ4jVXUwVOcMBQG8Dc7Rc4PUntdWV2uxjDpGaGeozOp1woar4jd5t/v9wLstMwsA/ntlxdCAgUc1hjTc9/JCZfGoo3wG3HdmdAWrIJRHe7GM/TmpY5VFefuVcAkkPbLIaN8rmPmjloyZxgyR3GuJ4K0AGtJ2htz8o7yqikm759fldQXaMpbDzjKAG+8v+AugVTOPO5DOjLvGtUYQwh0CPjnVMyGd+8/GfUB5nLKJDD2aLDh5HYyMDJGDwQIo2ZmZcKbowNmAdB/AzyFhrmF2MHRb0QJJfaAnwGB6orZhoykLzJtGwF/xpYxI1dswomiUj3gTuAIqCn/4C7cULwGNBtwMTk3Y4LfKB5YUaOKBKYtpplm7u0vip8tU1NWWyI/7XdcSuIDoMt6rVHMWT0DbjHPGqDqZVSa6zleLcUTcIKLoMv3ueJluALtAo9B302zPPlrtiVScRdCjXvVh3e3JpYa/jjkuC9N+LrBMlz/eAN4eQijX2EdLo6c5tGGHwLyHFtXk89dDGHwCVhG9T0S/j55AhRZgkMCmUQXJ49TnS1wnQDvw0eAh9ICeMmEFbCnPMFzjAvsWoEWEFdYEx+S0MoUZ1gT1wId8+AF3Bl2OoEu906AUHx5VLw/gXYg/x84loOah/2UYNrgiwSwGO7RfUzVBbx/kgpckumGOi6QirtD6gkLTitbnxNol47S2jVc2vsN5kPqaAHT8uUdAJM4v/DanjYOwmUjWznGfwB7sGtAtor5BgofDuzaRj4kSQAqDakTsKORa3Q3xKi3gE1fhl71KRMqrdZ2AWNNg/YOhQyrVBnb+i+nEg4bsDA+egAAAABJRU5ErkJggg==
Subject Re: RegExp problem
Newsgroups comp.lang.javascript
References <9608f578-525d-4670-9792-0700df15eb41@googlegroups.com>
MIME-Version 1.0
Lines 54
NNTP-Posting-Date 14 Mar 2013 17:31:31 CET
NNTP-Posting-Host 8ccbce5f.newsspool2.arcor-online.net
X-Trace DXC=EW?\2O@_]LcfF8a^:6>b7eA9EHlD;3Ycb4Fo<]lROoRa8kF<OcfhCOkS6]dJ=QET;aDZm8W4\YJNlb@mF9jNikdeBoKGPB=12AgX8nlbMRgi_m
X-Complaints-To usenet-abuse@arcor.de
Xref csiph.com comp.lang.javascript:18893

Show key headers only | View raw


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


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