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


Groups > comp.lang.javascript > #18887

Re: RegExp problem

From "Jukka K. Korpela" <jkorpela@cs.tut.fi>
Newsgroups comp.lang.javascript
Subject Re: RegExp problem
Date 2013-03-14 14:22 +0200
Organization A noiseless patient Spider
Message-ID <khsfbf$udt$1@dont-email.me> (permalink)
References <9608f578-525d-4670-9792-0700df15eb41@googlegroups.com>

Show all headers | View raw


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/

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next 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