We help IT Professionals succeed at work.

Regular Expressions

BChan
BChan asked
on
Medium Priority
656 Views
Last Modified: 2012-06-27
Hello all,
I new to regular expressions, I want to know if a pattern can be created that will only match paterns not surrounded by "<%="  and  "%>".
Let me expalin what I am trying to do:
Given a page of HTML, I want to transform it into ASP response.write statements that will acomplish the same task. So
<HTML>
 <HEAD attribute="val">
<TITLE><%= Titlevar1 & "-" & Titlevar1 %></TITLE>
 </HEAD>
</HTML>
is changed into:
RESPONSE.write ("<HTML>" & vbNewLine)
RESPONSE.write ("<HEAD attribute=""val"" >" & vbNewLine)
RESPONSE.write ("<TITLE>" & Titlevar1 & "-" & Titlevar2  & "</TITLE>" & vbNewLine)
RESPONSE.write ("</HEAD>" & vbNewLine)
RESPONSE.write ("</HTML>" & vbNewLine)


I need to wrap the response.write around each line and replace all "'s with ""'s except when they are between <%= and %>.

The function below does all I need except this last requirement.
In need to improve
          pattern =/(\"){1}/gmi      
to not return the quotes that meet the  <% =  %> condition.

I would like to further improve this script to completly ignore ASP logic b/t <% and %>.

I am flexible w\ browsers  I wrote this one for ie 5.5.
function Transform()
{
     var f= document.frm
     var str = f.textarea1.value + ' '    
       var pattern
       //find all quotes and double them      
       pattern =/(\"){1}/gmi      
     str = str.replace(pattern,'$1"');
     
     //Make all <%= %> into vaiables " " & var & "  "
     //EX: <TITLE><%= titlevar %></TITLE>
     //     Becomes: <TITLE>" & titlevar & "</TITLE>
     pattern =/<%(?:.*)[=](?: *)(.*)%>/gi
       str = str.replace(pattern,'" & $1 & "');
     pattern =/(.+)(?:.)/g
     str = str.replace(pattern,'Response.Write("$1" & VbNewLine)');
     f.textarea1.value = str;
}
Comment
Watch Question

Commented:
you can improve your function so that you will repeat it for each section of the page that is between the tags. I mean you will find (using function indexOf) the first index of "<%=" and run your code on str till that point, then look for the next "%>" index and continue from there, till you will not find any more "<%=" in str.

note that "<%=" can olso be written "<% =" or in any other white space between them.

Is that improvement is good enough for your needs?

Author

Commented:
The solution you suggest will be my last resort. I was hoping to avoid that looping - the more complex the routine becomes, The more likely I am to inadvertently affect the code I am parsing.

Also, I am also learning the limits of reg expressions. I may have found them though.

I have also posted this question in the Perl section of the site:
http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=perl&qid=20240196

I will accept this answer if the task cannot be done with regular expressions or, in a less complex mannor in Perl.

Thanks for your response!
BChan

Author

Commented:
PC_User321 - CLean up work - i accecpted your awnser on the question that I atttacheed no points to. Thanks for your help! These points are long over due!

Explore More ContentExplore courses, solutions, and other research materials related to this topic.