Skip to main content
Participating Frequently
October 1, 2007
Question

RegExp multiline

  • October 1, 2007
  • 5 replies
  • 4930 views
Can anyone make RegExp multiline work ? So far I've been replacing \n and \r with <cr> to flatten the string, then switching back the CRs.
..
var re = new RegExp("(.*)<!-- OrdinarySiteMap -->.*<!-- OrdinarySiteMap -->(.*)","m");

RegExp.multiline = true;
re.multiline = true;

var Slurp = re.exec(ExistOSMfile);

if (Slurp) {
var Topper = Slurp[1] + '<!-- OrdinarySiteMap -->';
var Bottomer = '<!-- OrdinarySiteMap -->' + Slurp[2];
...
This topic has been closed for replies.

5 replies

Participating Frequently
October 5, 2007
Thanks to Randy and Danilo for their attention to this topic.

I've used this regexp with success

<snip>
tempfile = (/^((?:.|\r|\n)*)OrdinarySiteMap(.*) OrdinarySiteMap((?:.|\r|\n)*)$/.exec(ExistOSMfile));

if (! DWfile.write(localSiteURL+ "temp.html", tempfile[1]+
"xxxxxxxxxxxxxxxxxxmiddlexxxxxxxxxxxx" +tempfile[3] )){
alert("Write Failed: " + localSiteURL + "temp.html");
}
</snip>

The (?:) extension is not in Javascript 1.3 (c)1999 from the former devedge.netscape.com which is archived in one form or another at
http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Guide:Creating_a_Regular_Expression

http://perldoc.perl.org/perlre.html (which is pretty old) says things like:
...
This is for clustering, not capturing; it groups subexpressions like "()", but doesn't make backreferences as "()" does. So...
The stability of these extensions varies widely...
...

I've never checked out Microsoft perversion on it but there is something here:
http://msdn2.microsoft.com/en-us/library/yab2dx62.aspx

Peace out,
Jonny

Inspiring
October 3, 2007
danilocelic AdobeCommunityExpert wrote:
> This RegExp grabs the content from the beginning of the code before
> MARKER, and also the content after MARKER through the end of the code:
> /^((?:.|\r|\n)*)MARKER((?:.|\r|\n)*)$/

And based on Randy's latest post this Regular Expression also works:
/^([\s\S]*)MARKER([\s\S]*)$/

Goes to show there are many ways to handle doing regular expressions, and you can always learn a new way to do the same thing.


--
Danilo Celic
| Extending Knowledge Daily : http://CommunityMX.com/
| Adobe Community Expert
Inspiring
October 3, 2007
meshgraphics wrote:
> Attn: Tourists
>
> This newsgroup is for people who have actually written a DW extension, not
> feature requests for newbies.

Massimo has written quite a few extensions, and Randy is one of the engineers that works on Dreamweaver, so it might be worth backing off a bit with the attitude if you intend to get help from folks.

> The RexExp features listed at regularexpression.com do not matter when you are
> trying to troubleshoot your DW extension. What matters is: what actually works
> in the DW extension environment.

They do to a certain extent, as they refer to the use of regular expressions within JavaScript, which Dreamweaver does use in extensions. Note this page:
http://www.regular-expressions.info/javascript.html

It states:
/m enables "multi-line mode". In this mode, the caret and dollar match before and after newlines in the subject string.

It also states:
Notably absent is an option to make the dot match line break characters.

Based upon both of these statement, the RegExp you present below does not do what you want it to do in JavaScript, regardless of Dreamweaver's involvement or not. The m flag does not allow the . character to match new lines in JavaScript and so you won't get from the beginning of the input string, rather you'll get from the beginning of the line that MARKER happens to be on.

> The purpose of the original questions and code snippet was to grab the top
> portion of a file, up to the marker, in its original format, so that it could
> be written as is, to the output file. According to old js, something like this
> would work:
>
> Slurp = /(^.*)MARKER(.*?)/m.exec(MultilineInputString);
> write(Slurp[1]); // <html><head><body>
> write(Slurp[2]); // </body></html>

What "old js" are you referring to that says that this should work the way you are saying it should?

For this code:
<html>
<head>
<title>My Document</title>
</head>
<body>
MARKER
</body>
</html>

This RegExp grabs the content from the beginning of the code before MARKER, and also the content after MARKER through the end of the code:
/^((?:.|\r|\n)*)MARKER((?:.|\r|\n)*)$/




--
Danilo Celic
| Extending Knowledge Daily : http://CommunityMX.com/
| Adobe Community Expert
Participating Frequently
October 3, 2007
Attn: Tourists

This newsgroup is for people who have actually written a DW extension, not feature requests for newbies.

The DW extension environment is a subset of DOM level 1 and Microsoft Internet Explorer 4.0 DOM. This is far surpassed by modern web browser so testing your js in a web page doesn't help you with the finer points.
http://livedocs.adobe.com/en_US/Dreamweaver/9.0_Extending/index.html

The RexExp features listed at regularexpression.com do not matter when you are trying to troubleshoot your DW extension. What matters is: what actually works in the DW extension environment.

The purpose of the original questions and code snippet was to grab the top portion of a file, up to the marker, in its original format, so that it could be written as is, to the output file. According to old js, something like this would work:

Slurp = /(^.*)MARKER(.*?)/m.exec(MultilineInputString);
write(Slurp[1]); // <html><head><body>
write(Slurp[2]); // </body></html>

If you can make this work in a DW extension, let me know. Otherwise, enjoy the Brittney Spears news.

Jon Wojkowski
meshgraphics.com












Inspiring
October 1, 2007
meshgraphics,

Most people assume that the . wildcard means "match everything", but it
actually means "match eveything except \r and \n". Use (.\r\n) instead.

Another tip: most people also assume that * means match *first* (i.e.
smallest) match, but it actually means match *largest* match. This can
lead to undesired results and unnecessary performance penalty
(especially when searching across multiple lines). Use .*? or (.\r\n)*?
for minimum match.

HTH,
Randy


> Can anyone make RegExp multiline work ? So far I've been replacing \n and \r
> with <cr> to flatten the string, then switching back the CRs.
> ..
> var re = new RegExp("(.*)<!-- OrdinarySiteMap -->.*<!-- OrdinarySiteMap
> -->(.*)","m");
>
> RegExp.multiline = true;
> re.multiline = true;
>
> var Slurp = re.exec(ExistOSMfile);
>
> if (Slurp) {
> var Topper = Slurp[1] + '<!-- OrdinarySiteMap -->';
> var Bottomer = '<!-- OrdinarySiteMap -->' + Slurp[2];
> ...
Participating Frequently
October 2, 2007
Ok Randy. Do most people know how multiline RegExp works? I don't see any evidence that you do.