Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!us.feeder.erje.net!feeder.erje.net!eu.feeder.erje.net!newsreader4.netcologne.de!news.netcologne.de!newsfeed.arcor.de!newsspool3.arcor-online.net!news.arcor.de.POSTED!not-for-mail Content-Type: text/plain; charset="UTF-8" Message-ID: <16779241.CEys5BfS3R@PointedEars.de> From: Thomas 'PointedEars' Lahn Reply-To: Thomas 'PointedEars' Lahn Organization: PointedEars Software (PES) Date: Sat, 12 Jan 2013 16:16:14 +0100 User-Agent: KNode/4.4.11 Content-Transfer-Encoding: 8Bit Subject: Re: Need regexp help Newsgroups: comp.lang.javascript References: Followup-To: comp.lang.javascript MIME-Version: 1.0 Lines: 60 NNTP-Posting-Date: 12 Jan 2013 16:16:15 CET NNTP-Posting-Host: 357b4895.newsspool3.arcor-online.net X-Trace: DXC=>nDV\_HCIMnJ00P1S40fZgMcF=Q^Z^V3h4Fo<]lROoRa8kFYBYU3JEom On Fri, 11 Jan 2013 17:14:16 -0800, Richard Yates > wrote: >> justaguy wrote: >>> How could use regexp (replace) to find all occurences of the following ^^^ >>> CSS code in a long string and remove them? Thanks. >>> >>> >>> 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 } >> >> Not sure why it's a regexp problem. Use indexOf() to find the location >> of the section you want to remove, and then a couple substring() to >> close up the gap. > > var ind = haystack.indexOf(needle); > var newstring = > haystack.substring(0,ind)+haystack.substring(ind+needle.length); The key word here is “all”. Your code would replace only *one* occurrence, so to replace *all* occurrences you would need to run it in a loop. We had to do comparably inefficient stuff like that before regular expressions were introduced in ECMAScript Edition 3. That was a little more than 13 years ago (1999-12). (But even then we used String.prototype.replace() already.) That said, one should not use regular expressions on a context-free language unless one really knows what one is doing. 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. BTW, the code to remove is not Valid (there is no “valign” CSS property to begin with; the property name is “vertical-align” [2]). But that could be one reason why it is to be removed. _________ [1] [2] -- PointedEars Twitter: @PointedEars2 Please do not Cc: me. / Bitte keine Kopien per E-Mail.