Skip to content

Instantly share code, notes, and snippets.

@orangexception
Created October 20, 2011 13:33
Show Gist options
  • Select an option

  • Save orangexception/1301150 to your computer and use it in GitHub Desktop.

Select an option

Save orangexception/1301150 to your computer and use it in GitHub Desktop.

Revisions

  1. orangexception revised this gist Oct 20, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@ This regular expression was designed to minify simple JavaScript.

    I put spaces in square brackets as a reminder that they exist. Spaces can be important. For example, `$( "#foo #bar" )` should not become `$("#foo#bar")`. However, we do want to remove spaces if they are used for indentation.

    I'm having trouble removing // comments due to values such as "http://."
    I'm having trouble removing // comments due to values such as "http://." I'm having some success with `(?s)(?m)[\t\r\n]|[ ][ ]+|/\*.*?\*/|^//[^\r\n]*|[^\\"":]//[^\r\n]*`, but I need to build more test cases.

    # Usage in ColdFusion
    `sJavaScriptContent= sJavaScriptContent.ReplaceAll( "(?s)[\t\r\n]|[ ][ ]+|/\*.*?\*/**" , "" );`
  2. orangexception revised this gist Oct 20, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,7 @@ I put spaces in square brackets as a reminder that they exist. Spaces can be imp
    I'm having trouble removing // comments due to values such as "http://."

    # Usage in ColdFusion
    `sJavaScriptContent= sJavaScriptContent.ReplaceAll( "(?s)\s|/\*.*?\*/|//[^\r\n]*" , "" );`
    `sJavaScriptContent= sJavaScriptContent.ReplaceAll( "(?s)[\t\r\n]|[ ][ ]+|/\*.*?\*/**" , "" );`

    # Test Cases
    ```
  3. orangexception revised this gist Oct 20, 2011. 1 changed file with 13 additions and 32 deletions.
    45 changes: 13 additions & 32 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,16 @@
    # Notice
    Do not run this against minified JavaScript packages. The minified packages often rely on line returns to execute correctly.

    This regular expression was designed to minify simple JavaScript.

    # Regular Expression
    `(?s)\s|/\*.*?\*/|//[^\r\n]*`
    `(?s)[\t\r\n]|[ ][ ]+|/\*.*?\*/*`

    # Usage in ColdFusion
    I've tested with ColdFusion 8 and Railo 3.3.
    I put spaces in square brackets as a reminder that they exist. Spaces can be important. For example, `$( "#foo #bar" )` should not become `$("#foo#bar")`. However, we do want to remove spaces if they are used for indentation.

    I'm having trouble removing // comments due to values such as "http://."

    # Usage in ColdFusion
    `sJavaScriptContent= sJavaScriptContent.ReplaceAll( "(?s)\s|/\*.*?\*/|//[^\r\n]*" , "" );`

    # Test Cases
    @@ -25,33 +32,7 @@ test 3
    */
    // ////// Test 9
    // */ Test 10
    ```

    ## Complete Test
    ```
    <cfsavecontent variable= "sJavaScriptContent">
    // Test 0
    /* Test 1 */
    /* Test * / * 2 */
    /* Test
    . test
    . test
    test 3
    */
    /* Test * / * 4 */ /* Test 5 *//**/
    /* Test 6 /*/
    // Test 7
    /* Test
    // 8
    */
    // ////// Test 9
    // */ Test 10
    alert( 'end-of-line' );
    </cfsavecontent>
    <cfset sJavaScriptContent= sJavaScriptContent.ReplaceAll( "(?s)\s|/\*.*?\*/|//[^\r\n]*" , "" ) />
    <script>
    <cfoutput>#sJavaScriptContent#</cfoutput>
    </script>
    $( "#foo #bar" ).val( "Test 11 - This value should exist." );
    $( "#foo #bar" ).val( "http://orangexception.com/" ); // Test 12 - http:// should exist. However, this comment should not exist. //
    // Test 13 - This line should be gone. http://orangexception.com/foo/bar/
    ```
  4. orangexception revised this gist Oct 20, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@
    # Usage in ColdFusion
    I've tested with ColdFusion 8 and Railo 3.3.

    sJavaScriptContent= sJavaScriptContent.ReplaceAll( "(?s)\s|/\*.*?\*/|//[^\r\n]*" , "" );
    `sJavaScriptContent= sJavaScriptContent.ReplaceAll( "(?s)\s|/\*.*?\*/|//[^\r\n]*" , "" );`

    # Test Cases
    ```
  5. orangexception created this gist Oct 20, 2011.
    57 changes: 57 additions & 0 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,57 @@
    # Regular Expression
    `(?s)\s|/\*.*?\*/|//[^\r\n]*`

    # Usage in ColdFusion
    I've tested with ColdFusion 8 and Railo 3.3.

    sJavaScriptContent= sJavaScriptContent.ReplaceAll( "(?s)\s|/\*.*?\*/|//[^\r\n]*" , "" );

    # Test Cases
    ```
    // Test 0
    /* Test 1 */
    /* Test * / * 2 */
    /* Test
    . test
    . test
    test 3
    */
    /* Test * / * 4 */ /* Test 5 *//**/
    /* Test 6 /*/
    // Test 7
    /* Test
    // 8
    */
    // ////// Test 9
    // */ Test 10
    ```

    ## Complete Test
    ```
    <cfsavecontent variable= "sJavaScriptContent">
    // Test 0
    /* Test 1 */
    /* Test * / * 2 */
    /* Test
    . test
    . test
    test 3
    */
    /* Test * / * 4 */ /* Test 5 *//**/
    /* Test 6 /*/
    // Test 7
    /* Test
    // 8
    */
    // ////// Test 9
    // */ Test 10
    alert( 'end-of-line' );
    </cfsavecontent>
    <cfset sJavaScriptContent= sJavaScriptContent.ReplaceAll( "(?s)\s|/\*.*?\*/|//[^\r\n]*" , "" ) />
    <script>
    <cfoutput>#sJavaScriptContent#</cfoutput>
    </script>
    ```