简体   繁体   English

CFML RegEx删除JavaScript注释

[英]CFML RegEx to remove javascript comments

I am looking to remove javascript comments from a string using coldfusion. 我正在寻找使用Coldfusion从字符串中删除JavaScript注释。 I am currently using reReplace(string, "(\\/\\*.*\\*\\/)|\\s(\\/\\/.{1,}[\\r\\n])", "", "all") . 我目前正在使用reReplace(string, "(\\/\\*.*\\*\\/)|\\s(\\/\\/.{1,}[\\r\\n])", "", "all")

This is a test string: 这是一个测试字符串:

<script type="text/javascript">
// comment
var a=1; // another comment
/* try{if (...)}; */
var b=2;
</script>
src="//domain.com"

The expected result is (and what I get using replace() in javacript): 预期的结果是(以及我在javacript中使用replace()得到的结果):

<script type="text/javascript">
var a=1; 
var b=2;
</script>
src="//domain.com"

Actual CFML results: 实际CFML结果:

<script type="text/javascript">
src="//domain.com"

Again, it works in javascript OK. 同样,它可以在javascript中正常运行。

How to get this working in CFML? 如何使它在CFML中工作?


UPDATE 1, more specific code in my app. 更新1,我的应用程序中有更具体的代码。 It's basically a minifier within app.cfc's OnRequest() function. 从本质上讲,它是app.cfc的OnRequest()函数中的一个减少器。

  1. Get the page html 获取页面html
  2. Remove both types of JS comments 删除两种类型的JS注释
  3. Flatten \\r\\n to \\r 将\\ r \\ n展平为\\ r
  4. Replace \\n+\\t to a space 将\\ n + \\ t替换为空格
  5. Replace \\t to a space 将\\ t替换为空格
  6. Replace double spaces with a single space 用一个空格替换两个空格
  7. Replace double \\r with a single \\r 用单\\ r代替双\\ r
  8. Replace comma+\\r with a comma 用逗号替换逗号+ \\ r

     <!--- Define arguments. ---> <cfargument name="TargetPage" type="string" required="true" /> <cfheader name="content-type" value="text/html; charset=utf-8" /> <cfheader name="X-UA-Compatible" value="IE=edge" /> <cfheader name="window-target" value="_top" /> <cfheader name="imagetoolbar" value="no" /> <cfheader name="viewport" value="wwidth=device-width, initial-scale=1, maximum-scale=1, user-scalable=0" /> <cfsavecontent variable="finalContent"> <cfinclude template="#ARGUMENTS.TargetPage#" /> </cfsavecontent> <cfset variables.regex = '(?:("\\/\\/[^"]*?")|\\/\\*.*?\\*\\/|\\/\\/.*?\\n)'> <!--- <cfset finalContent = reReplace(finalContent,variables.regex, "\\1", "ALL")> ---> <cfset finalContent = replace(finalContent, chr(13), chr(10), 'all')> <cfset finalContent = replace(finalContent, chr(10)&chr(9), ' ', 'all')> <cfset finalContent = replace(finalContent, chr(9), ' ', 'all')> <cfloop from="1" to="20" index="e"> <cfset finalContent = replace(finalContent, ' ', ' ', 'all')> <cfset finalContent = replace(finalContent, chr(10)&chr(10), chr(10), 'all')> </cfloop> <cfset finalContent = replace(finalContent, ','&chr(10), ',', 'all')> <cfset finalContent = replace(finalContent, chr(10), '', 'all')> <cfoutput>#finalContent#</cfoutput> <cfreturn /> 

And some true (but truncated) output to play with: 和一些真实的(但被截断的)输出可用于:

<script src="//code.jquery.com/jquery-2.1.4.min.js"></script>
<script type="text/javascript">
//<![CDATA[
try{if (...) {...;}} catch(e){};
//]]>
// comment
var a=1; // another comment
/* try{if (...)}; */
var b=2;
</script>
<script type="text/javascript">
 unsavedChanges=0;
 tinymce.init({
     // GENERAL
     // PLUGINS
     // LINK
     link_list: "/pagesJSON.cfm", target_list: [
         {title: 'Same Window/Tab', value: '_self'}, {title: 'New Window/Tab', value: '_blank'}
     ],
     // FILE MANAGER
     external_filemanager_path: '/filemanager/',
     // IMAGE
     image_advtab: true
 });
 </script>
<link rel='stylesheet' href='https://fonts.googleapis.com/css?family=Lato%3A400%2C700%2C900&#038;ver=4.3.1' type='text/css' media='all'/>

You can try this: 您可以尝试以下方法:

<!--- JS with comment --->
<cfsavecontent variable="variables.jsWithCommment">
    <script type="text/javascript">
    // comment
    var a=1; // another comment
    /* try{if (...)}; */
    var b=2;
    </script>
    src="//domain.com"
</cfsavecontent>

<!--- Replace with first capture for each branch --->
<cfset variables.regex = '(?:("\/\/[^"]*?")|\/\*.*?\*\/|\/\/.*?\n)'>
<cfset variables.jsWithoutComment = reReplace(variables.jsWithCommment, variables.regex, "\1", "ALL")>

Regex: 正则表达式:

Branch 1: ("\/\/[^"]*?")  ==> Capture(to replace with same later i.e., \1) URL shorthand
Branch 2: \/\*.*?\*\/     ==> MultiLine Comment
Branch 3: \/\/.*?\n       ==> SingleLine Comment

Here is the TryCF . 这是TryCF

Solution by OP. 由OP解决。

The correct reReplace is: 正确的替换是:

reReplace(finalContent, '\/\*.*?\*\/|\s(\/\/.*?\r\n)', "", "ALL")

Makes the below output. 进行以下输出。 Still needs some cleaning but links and js functions don't break! 仍然需要清理,但是链接和js函数不会中断!

<script src="//code.jquery.com/jquery-2.1.4.min.js"></script>
    <script type="text/javascript">
       try{if (...) {...;}} catch(e){};
          var a=1;    
    var b=2;
    </script>
    <script type="text/javascript">
     unsavedChanges=0;
     tinymce.init({
                                 link_list: "/pagesJSON.cfm", target_list: [
             {title: 'Same Window/Tab', value: '_self'}, {title: 'New Window/Tab', value: '_blank'}
         ],
                 external_filemanager_path: '/filemanager/',
                 image_advtab: true
     });
     </script>
    <link rel='stylesheet' href='https://fonts.googleapis.com/css?family=Lato%3A400%2C700%2C900&#038;ver=4.3.1' type='text/css' media='all'/> 

The regular expression pattern which you are currently trying seems to be incorrect, (I have tried validating it with the "Online RegEx Tester" and confirmed). 您当前正在尝试的正则表达式模式似乎不正确,(我尝试使用“ Online RegEx Tester”对其进行验证并确认)。

You need to rewrite it as, 您需要将其重写为

\\/\\*[\\s\\S]*?\\*\\/|([^:"]|^)\\/\\/.*$

Here is the screenshot from, https://regex101.com/#javascript (Where I had tested the above pattern) 这是https://regex101.com/#javascript的屏幕截图(我在上面测试过的模式)

在此处输入图片说明

Try using the \\/\\*[\\s\\S]*?\\*\\/|([^:"]|^)\\/\\/.*$ in your reReplace function which will workout for you. 尝试在reReplace功能中使用\\/\\*[\\s\\S]*?\\*\\/|([^:"]|^)\\/\\/.*$ ,它将为您锻炼。

Hope this helps you! 希望这对您有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM