What's wrong with this regex code that works fine in search/replace?

Help with writing and running scripts

What's wrong with this regex code that works fine in search/replace?

Postby caveatrob » Tue Aug 26, 2008 9:32 am

Getting an error here:

Code: Select all
var regexpFind=/.*TextData">(.|\r\n|\n)*</Column>/; /* use a regexp object and not a string, so we don't have to double-escape backslashes */


The expression works fine in find and replace.
caveatrob
Basic User
Basic User
 
Posts: 20
Joined: Fri Apr 25, 2008 4:39 pm

Re: What's wrong with this regex code that works fine in search/replace?

Postby Mofi » Tue Aug 26, 2008 10:35 am

I guess, the problem is the single double quote in your not double quoted search string.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4058
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: What's wrong with this regex code that works fine in search/replace?

Postby caveatrob » Tue Aug 26, 2008 10:37 am

The need is to match the data between the Column tags in an example shown below:


<Column id="1" name="TextData">SELECT ca.*, CASE c.boodle_minkyID WHEN 0
THEN CASE WHEN ca.name IS NULL THEN 0 ELSE c.minkyID END ELSE CASE
ca.isBranded WHEN 0 THEN 0 ELSE c.boodle_minkyID END END AS agencyID
FROM minky_dipsy AS ca WITH (NOLOCK), minky AS c WITH (NOLOCK)
WHERE ca.minkyID = c.minkyID AND ca.status = 1 AND ca.minkyID =
1</Column>

The column tag could also be on one line:

<Column id="1" name="TextData">SELECT ca.* from DorkNozzle</Column>
caveatrob
Basic User
Basic User
 
Posts: 20
Joined: Fri Apr 25, 2008 4:39 pm

Re: What's wrong with this regex code that works fine in search/replace?

Postby Mofi » Wed Aug 27, 2008 2:46 am

Because you need a double quote in your regular expression search string, you must define it as string or the JavaScript engine cannot interpret this line correct. Do you have no syntax highlighting activated for script files? As I copied your code line into a file with extension JS it immediately highlighted everything starting from the single double quote as string, a very good visual indication that there is something wrong in the syntax. Use

var regexpFind="/.*TextData\">(.|\\r\\n|\\n)*</Column>/";

and delete the comment which is not correct anymore for this line.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4058
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: What's wrong with this regex code that works fine in search/replace?

Postby jorrasdk » Wed Aug 27, 2008 3:40 am

You can still define the regexp as Regexp object. But not the double quote that is causing you problems (all though as Mofi describes it, it screws up the syntax highligting) but you need to escape the forward slash in </Column> because the script engine thinks the regexp is terminated at the first forward slash.

var regexpFind=/.*TextData">(.|\r\n|\n)*<\/Column>/;

If you configure UE with Javascript Lint it will duly report the error as:

Code: Select all
C:\...\temp.js(1): SyntaxError: invalid flag after regular expression
var regexpFind=/.*TextData">(.|\r\n|\n)*</Column>/;
..........................................^

1 error(s), 0 warning(s)
User avatar
jorrasdk
Master
Master
 
Posts: 275
Joined: Mon Mar 19, 2007 11:00 pm
Location: Denmark


Return to Scripts