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


Groups > comp.lang.javascript > #18097

Re: Need regexp help

Message-ID <1373174.86QkPN8Rvz@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Organization PointedEars Software (PES)
Date 2013-01-13 00:49 +0100
Subject Re: Need regexp help
Newsgroups comp.lang.javascript
References <eed9c2f1-0e66-4502-849d-9d538eddd1a8@googlegroups.com> <d1e1f8d6shakdd5sjro0vcudj1utmsa4q7@4ax.com> <vcr1f8ptnnqptopbi07esct071uuo38htl@4ax.com> <16779241.CEys5BfS3R@PointedEars.de>
Followup-To comp.lang.javascript

Followups directed to: comp.lang.javascript

Show all headers | View raw


Thomas 'PointedEars' Lahn wrote:

> […]  In this exceptional case a *single* regular expression would be
> appropriate to parse CSS because the probability of a false positive for
> pattern matching can be reduced to close to zero.  Watch for word-wrap (or
> instead use RegExp.prototype.concat(), provided by jsx.regexp.concat()
> [1]):
> 
>   css = css.replace(/(^|\s*)body\s*\{\s*font-family\s*:\s*"Courier New",
> \s*Courier,\s*monospace;\s*font-size\s*:\s*9pt;\s*valign\s*:\s*top;
\s*text-
> align\s*:\s*left;\s*line-height:\s*9pt\s*\}\s*td\s*\{\s*font-family\s*:
> \s*"Courier New",\s*Courier,\s*monospace;\s*font-size\s*:\s*9pt;
> \s*valign\s*:\s*top;text-align\s*:\s*left;\s*line-height:\s*9pt\s*\}/g,
> "$1");
> 
> This begs the question, however, why the OP thinks this would be necessary
> here in the first place.  I certainly would not recommend it.

A better readable version of this:

  var css = …;

  var needle = 'body { font-family : "Courier New", Courier, monospace;'
             + " font-size : 9pt; valign : top; text-align : left;"
             + " line-height: 9pt }"
             + ' td { font-family : "Courier New", Courier, monospace;'
             + " font-size : 9pt; valign : top; text-align : left;"
             + " line-height: 9pt }";

  var rxNeedle = new RegExp(
    "(^|\\s*)"
    + needle.replace(/("[^"]*")|\s+/g, function (match, doubleQuoted) {
        return doubleQuoted || "\\s*";
      }).replace(/[{}]/g, "\\$&"),
    "g");

  css = css.replace(rxNeedle, "");

See also RegExp.prototyp.escape() provided by jsx.regexp.escape(). [1]

> _________
> [1] <http://PointedEars.de/scripts/test/regexp>
-- 
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Need regexp help justaguy <lichunshen84@gmail.com> - 2013-01-11 17:01 -0800
  Re: Need regexp help Richard Yates <richard@yatesguitar.com> - 2013-01-11 17:14 -0800
    Re: Need regexp help Richard Yates <richard@yatesguitar.com> - 2013-01-11 21:00 -0800
      Re: Need regexp help Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-01-12 16:16 +0100
        Re: Need regexp help "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2013-01-12 16:27 +0100
        Re: Need regexp help Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-01-13 00:49 +0100
  Re: Need regexp help Luc Yen <luc@goal.tw> - 2013-01-11 22:10 -0800
  Re: Need regexp help "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2013-01-12 09:25 +0100
    Re: Need regexp help justaguy <lichunshen84@gmail.com> - 2013-01-12 06:33 -0800
  Re: Need regexp help justaguy <lichunshen84@gmail.com> - 2013-01-12 10:22 -0800
    Re: Need regexp help justaguy <lichunshen84@gmail.com> - 2013-01-12 10:48 -0800
      Re: Need regexp help SAM <stephanemoriaux.NoAdmin@wanadoo.fr.invalid> - 2013-01-13 22:02 +0100
    Re: Need regexp help "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2013-01-12 23:11 +0100

csiph-web