Note that there are some explanatory texts on larger screens.

plurals
  1. POReplacing html script element sources with regular expression in Python
    text
    copied!<p>I'm trying to write a large python/bash script which converts my html/css mockups to Shopify themes. One step in this process is changing out all the script sources. For instance:</p> <pre><code>&lt;script type="text/javascript" src="./js/jquery.bxslider.min.js"&gt;&lt;/script&gt; </code></pre> <p>becomes</p> <pre><code>&lt;script type="text/javascript" src="{{ 'jquery.bxslider.min.js' | asset_url }}"&gt;&lt;/script&gt; </code></pre> <p>Here is what I have so far:</p> <pre><code>import re test = """ &lt;script type="text/javascript" src="./js/jquery-1.8.3.min.js"&gt;&lt;/script&gt; &lt;!--&lt;script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" type="text/javascript"&gt;&lt;/script&gt;--&gt; &lt;script type="text/javascript" src="./js/ie-amendments.js"&gt;&lt;/script&gt; &lt;script type="text/javascript" src="./js/jquery.bxslider.min.js"&gt;&lt;/script&gt; &lt;script type="text/javascript" src="./js/jquery.colorbox-min.js"&gt;&lt;/script&gt; &lt;script type="text/javascript" src="./js/main.js"&gt;&lt;/script&gt; """ out = re.sub( 'src=\"(.+)\"', 'src="{{ \'\\1\' | asset_url }}"', test, flags=re.MULTILINE ) out </code></pre> <p>prints out</p> <pre><code>'\n &lt;script type="text/javascript" src="{{ \'./js/jquery-1.8.3.min.js\' | asset_url }}"&gt;&lt;/script&gt;\n &lt;!--&lt;script src="{{ \'http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" type="text/javascript\' | asset_url }}"&gt;&lt;/script&gt;--&gt;\n &lt;script type="text/javascript" src="{{ \'./js/ie-amendments.js\' | asset_url }}"&gt;&lt;/script&gt;\n &lt;script type="text/javascript" src="{{ \'./js/jquery.bxslider.min.js\' | asset_url }}"&gt;&lt;/script&gt;\n &lt;script type="text/javascript" src="{{ \'./js/jquery.colorbox-min.js\' | asset_url }}"&gt;&lt;/script&gt;\n &lt;script type="text/javascript" src="{{ \'./js/main.js\' | asset_url }}"&gt;&lt;/script&gt;\n' </code></pre> <p>I have two problems so far:</p> <ol> <li><p>Some of the backslash characters I'm using to escape the single quotes within my regexes are showing up in the output.</p></li> <li><p>My capture group is capturing the entire original source string, but I just need what comes after the last "/"</p></li> </ol> <p><strong>ANSWER</strong>: Per Martijn Pieters helpful suggestion, I checked out the ? regular expression operator, and came up with this solution, which perfectly solved my problem. Also, for the replacement expression, I encapsulated it in double-quotes as opposed to singles, and escaped the doubles, which ended up removing the unnecessary backslashes. Thanks guys!</p> <pre><code>re.sub( r'src=".+?([^/]+?\.js)"', "src=\"{{ '\\1' | asset_url }}\"", test, flags=re.MULTILINE ) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload