locked
URL Rewrite module - Pattern matching of HTML using regex - handle repeating or duplicate elements RRS feed

  • Question

  • User-1968421407 posted

    I am trying to inject

    <script src="...">

    to end of an HTML response using the IIS URL rewrite module extension.

    My HTML looks like this:

    <html> 
    <head> 
    <script id="iframeTmpl" type="text/template">
    <html><head>{0}</head><body>{1}</body></html>
    </script>
    </head>
    <body>
    Hello world!!!
    </body> 
    </html>
    

    What I want to achieve is this (note the <script> tag appended in the last line):

    <html> 
    <head> 
    <script id="iframeTmpl" type="text/template">
    <html><head>{0}</head><body>{1}</body></html>
    </script>
    </head>
    <body>
    Hello world!!!
    </body> 
    </html>
    <script src="/jquery.js"></script>
    

    In order to accomplish the above, I created an OutBound Rule with the following pattern in order to find the last occurrence of the </html> tag.:

    <\/html>(?![\s\S]*<\/html>[\s\S]*$) 

    Subsequently, in the Action Section of the URL Rewrite module, I gave the following value in order to append the <script> to the </html> tag.

    {R:0}<script src="/jquery.js"></script>


    However, the <script> is appended to all occurrences of </html> tags.

    So, what I see in the output is this:

    <html> 
    <head> 
    <script id="iframeTmpl" type="text/template">
    <html><head>{0}</head><body>{1}</body></html> <script src="/jquery.js"></script>
    </script>
    </head>
    <body>
    Hello world!!!
    </body> 
    </html>
    <script src="/jquery.js"></script>
    

    In order to validate the regex, I used the regex tester: https://regex101.com/r/mY5bY7/2. It appears to append the script in exactly the right place (i.e. the last occurring </html> element).

    Is there something wrong with the regex as used for IIS Rewrite module? Would highly appreciate any suggestions.

    Thursday, June 2, 2016 10:12 AM

Answers

  • User1278090636 posted

    Hi,

    Please change your regex pattern from

    <\/html>(?![\s\S]*<\/html>[\s\S]*$) 

    To

    <\/html>$

    I tested this regex pattern on my side, it works as expected. Below is my outbound rule code:

    <rule name="test out" patternSyntax="ECMAScript">
        <match filterByTags="None" pattern="&lt;\/html>$" />
        <action type="Rewrite" value="{R:0}&lt;script src=&quot;/jquery.js&quot;>&lt;/script>" />
    </rule>

    Best Regards,

    Jean

    • Marked as answer by Anonymous Tuesday, September 28, 2021 12:00 AM
    Tuesday, June 7, 2016 7:33 AM

All replies

  • User1278090636 posted

    Hi,

    You need to change your regex pattern to only match the last </html>in content.

    Please try replacing your regex pattern with "</html>$"

    Best Regards,

    Jean

    Friday, June 3, 2016 5:29 AM
  • User-1968421407 posted

    Jean,

    First of all thanks for your kind reply.

    As you suggested I changed the regex pattern from  

    <\/html>(?![\s\S]*<\/html>[\s\S]*$) 

    to

    <\/html>(?![\s\S]*<\/html>$)

    But it not working.  This regex pattern injects the script in all occurrence of </html> tags.

    As a result your regex pattern I am the getting result  as below. The script injected twice i.e. all occurrence of </html> tag. 

    <html> 
    <head> 
    <script id="iframeTmpl" type="text/template">
    <html><head>{0}</head><body>{1}</body></html> <script src="/jquery.js"></script>
    </script>
    </head>
    <body>
    Hello world!!!
    </body> 
    </html>
    <script src="/jquery.js"></script>

    And also I checked this regex pattern in online https://regex101.com/r/mY5bY7/3 . Its not working.

    I am repeating my requirement again, there may lot of </html> tags occurrence in a page, I need to inject at the last occurrence of </html> tag.

    See the below code the </html> tag  occurrence is two times. But the script should get injected at the last occurrence of </html>tag.  I deserved the below result.

    <html> 
    <head> 
    <script id="iframeTmpl" type="text/template">
    <html><head>{0}</head><body>{1}</body></html>
    </script>
    </head>
    <body>
    Hello world!!!
    </body> 
    </html>
    <script src="/jquery.js"></script>

    Please make sure that regex pattern works on both IIS and https://regex101.com/r/mY5bY7/3.

    Monday, June 6, 2016 10:21 AM
  • User1278090636 posted

    Hi,

    Please change your regex pattern from

    <\/html>(?![\s\S]*<\/html>[\s\S]*$) 

    To

    <\/html>$

    I tested this regex pattern on my side, it works as expected. Below is my outbound rule code:

    <rule name="test out" patternSyntax="ECMAScript">
        <match filterByTags="None" pattern="&lt;\/html>$" />
        <action type="Rewrite" value="{R:0}&lt;script src=&quot;/jquery.js&quot;>&lt;/script>" />
    </rule>

    Best Regards,

    Jean

    • Marked as answer by Anonymous Tuesday, September 28, 2021 12:00 AM
    Tuesday, June 7, 2016 7:33 AM