dcsimg
Results 1 to 16 of 16

Thread: How to remove all extra spaces, blank lines, and CrLfs in JavaScript code?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2012
    Posts
    2,038

    How to remove all extra spaces, blank lines, and CrLfs in JavaScript code?

    I want to use RegExp to remove all extra spaces, blank lines, and carriage returns and line breaks in JavaScript code to achieve the purpose of compressing JavaScript code. Is there some simple way to achieve this? Thanks!

    Edit:
    Solutions that do not use RegExp are also acceptable, which means that any method is acceptable and worth discussing.
    Last edited by dreammanor; Jan 15th, 2020 at 06:11 AM.

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,793

    Re: How to remove all extra spaces, blank lines, and CrLfs in JavaScript code?

    Commonly used mechanism is a utility called minify ... I think it will also do some semblance of obfuscation in the process as a byproduct... but it really compacts things down pretty tightly. I've painfully deconstructed scripts that have been through it... It's pretty widely used.


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,793

    Re: How to remove all extra spaces, blank lines, and CrLfs in JavaScript code?

    the reason the obfuscation is a side effect is because as part of the minification, variables are renamed from `yourVeryLongVariableName` to `a` to help shrink things down.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2012
    Posts
    2,038

    Re: How to remove all extra spaces, blank lines, and CrLfs in JavaScript code?

    Quote Originally Posted by techgnome View Post
    Commonly used mechanism is a utility called minify ... I think it will also do some semblance of obfuscation in the process as a byproduct... but it really compacts things down pretty tightly. I've painfully deconstructed scripts that have been through it... It's pretty widely used.
    Yes. I've tested UglifyJS and it works fine under Windows 10, but it does not work under XP. I've spent a lot of time and effort on compressing JavaScript code, and in the end, I had to take the worst approach: write a JavaScript code compression program from scratch.


    Quote Originally Posted by techgnome View Post
    the reason the obfuscation is a side effect is because as part of the minification, variables are renamed from `yourVeryLongVariableName` to `a` to help shrink things down.
    This is exactly what I need, because I want the published web pages to be as small as possible and as fast as possible.
    Last edited by dreammanor; Jan 15th, 2020 at 08:32 AM.

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,793

    Re: How to remove all extra spaces, blank lines, and CrLfs in JavaScript code?

    works fine under Windows 10, but it does not work under XP
    That makes no sense.. unless you're changing browsers...but you minify the code once.... and publish the minified code to the website.... so it shouldn't matter what OS you're using... Unless you mean the results didn't work... but then you may need to look at what your'e doing and what is/isn't supported by the browser... but that's browser-based not OS-based.



    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    2,150

    Re: How to remove all extra spaces, blank lines, and CrLfs in JavaScript code?

    Quote Originally Posted by techgnome View Post
    That makes no sense.. unless you're changing browsers...but you minify the code once.... and publish the minified code to the website.... so it shouldn't matter what OS you're using... Unless you mean the results didn't work... but then you may need to look at what your'e doing and what is/isn't supported by the browser... but that's browser-based not OS-based.



    -tg
    Ah, tg, but it makes sense, if you presume that XP is his developing-machine.....
    And yes, that is another can of worms......
    One System to rule them all, One IDE to find them,
    One Code to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    For health reasons i try to avoid reading unformatted Code

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    34,893

    Re: How to remove all extra spaces, blank lines, and CrLfs in JavaScript code?

    This doesn't seem to have anything to do with VB6 anymore, so you are likely to get better responses here. Minification is what you are trying to achieve. The tools are out there, and are likely to do better than anything you would write yourself, so the question is just about finding one that works on your development machine, which isn't a VB6 question.
    My usual boring signature: Nothing

  8. #8
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    2,410

    Re: How to remove all extra spaces, blank lines, and CrLfs in JavaScript code?

    Shaggy was quicker moving this thread

    here a start with removing blank/empty lines
    I tested it with loading a Textfile to a Richtextbox

    here the code
    Code:
    Option Explicit
    
    Public Function ReadFile(ByRef Path As String) As String
       Dim FileNr As Long
       On Error Resume Next
       If FileLen(Path) = 0 Then Exit Function
       On Error GoTo 0
       FileNr = FreeFile
       Open Path For Binary As #FileNr
       ReadFile = Space$(LOF(FileNr))
       Get #FileNr, , ReadFile
       Close #FileNr
    End Function
    
    Private Sub Command1_Click()
    RichTextBox1.text = ReadFile("D:\Textd.txt")
    End Sub
    
    Private Sub Command2_Click()
    RichTextBox2.text = fChangeText(RichTextBox1.text)
    End Sub
    
    Public Function fChangeText(sText As String) As String
        Dim regex As Object
        Set regex = CreateObject("Vbscript.Regexp")
        With regex
            .Pattern = "^\s*$[\r\n]*" 'remove empty lines
            .MultiLine = True
            .Global = True
            fChangeText = .Replace(sText, "")
        End With
        Set regex = Nothing
    End Function
    don't know if you want to do this further

    hth
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2012
    Posts
    2,038

    Re: How to remove all extra spaces, blank lines, and CrLfs in JavaScript code?

    Quote Originally Posted by Shaggy Hiker View Post
    This doesn't seem to have anything to do with VB6 anymore, so you are likely to get better responses here. Minification is what you are trying to achieve. The tools are out there, and are likely to do better than anything you would write yourself, so the question is just about finding one that works on your development machine, which isn't a VB6 question.
    Hi @Shaggy Hiker, this thread is about VB6. I hope I can achieve my goal in the following ways:
    (1) Pure VB6
    (2) VB6 + VBScript.RegExp
    (3) VB6 + RC5
    (4) VB6 + third-party components
    (5) VB6 + any other methods

    (6) Of course, pure JavaScript methods are also acceptable.

    In fact, any questions I ask on this forum are related to VB6. I want to use VB6 to accomplish everything I need.
    Last edited by dreammanor; Jan 16th, 2020 at 08:10 AM.

  10. #10

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2012
    Posts
    2,038

    Re: How to remove all extra spaces, blank lines, and CrLfs in JavaScript code?

    Quote Originally Posted by ChrisE View Post
    Shaggy was quicker moving this thread

    here a start with removing blank/empty lines
    I tested it with loading a Textfile to a Richtextbox

    here the code
    Code:
    Option Explicit
    
    Public Function ReadFile(ByRef Path As String) As String
       Dim FileNr As Long
       On Error Resume Next
       If FileLen(Path) = 0 Then Exit Function
       On Error GoTo 0
       FileNr = FreeFile
       Open Path For Binary As #FileNr
       ReadFile = Space$(LOF(FileNr))
       Get #FileNr, , ReadFile
       Close #FileNr
    End Function
    
    Private Sub Command1_Click()
    RichTextBox1.text = ReadFile("D:\Textd.txt")
    End Sub
    
    Private Sub Command2_Click()
    RichTextBox2.text = fChangeText(RichTextBox1.text)
    End Sub
    
    Public Function fChangeText(sText As String) As String
        Dim regex As Object
        Set regex = CreateObject("Vbscript.Regexp")
        With regex
            .Pattern = "^\s*$[\r\n]*" 'remove empty lines
            .MultiLine = True
            .Global = True
            fChangeText = .Replace(sText, "")
        End With
        Set regex = Nothing
    End Function
    don't know if you want to do this further

    hth
    Hi ChrisE, your code is a good start. But there are many other complicated situations to consider when removing extra spaces in JavaScript text, such as:
    (1) Cannot remove spaces in "strings" (wrapped in single or double quotes)
    (2) Cannot remove spaces or blank lines in "multi-line strings" (strings wrapped with "` ").

  11. #11

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2012
    Posts
    2,038

    Re: How to remove all extra spaces, blank lines, and CrLfs in JavaScript code?

    Hi @Shaggy Hiker,
    Maybe this thread should be moved back to the VB6 sub-forum.

    Hi @ChrisE,
    I just uploaded a test program. If you are interested, please help check if it can be further optimized. Thanks.

    http://www.vbforums.com/showthread.p...=1#post5446537

  12. #12
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,195

    Re: How to remove all extra spaces, blank lines, and CrLfs in JavaScript code?

    Quote Originally Posted by dreammanor View Post
    Hi @Shaggy Hiker,
    Maybe this thread should be moved back to the VB6 sub-forum.

    Hi @ChrisE,
    I just uploaded a test program. If you are interested, please help check if it can be further optimized. Thanks.

    http://www.vbforums.com/showthread.p...=1#post5446537
    Randomish question about your other thread: why are you replacing let with var? Are you working with clients that do not support ECMAScript 2015?

    If so, you're now looking at not only minifying a file, you're also getting into transpiling territory, which is a whole other can of worms. I would either recommend not transpiling at all (IE don't replace let, const, etc.), otherwise it could be confusing with people, changing how their script is meant to work.

  13. #13

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2012
    Posts
    2,038

    Re: How to remove all extra spaces, blank lines, and CrLfs in JavaScript code?

    Quote Originally Posted by kfcSmitty View Post
    Randomish question about your other thread: why are you replacing let with var? Are you working with clients that do not support ECMAScript 2015?

    If so, you're now looking at not only minifying a file, you're also getting into transpiling territory, which is a whole other can of worms. I would either recommend not transpiling at all (IE don't replace let, const, etc.), otherwise it could be confusing with people, changing how their script is meant to work.
    Yes, my XP development environment does not support ECMAScript 2015. All my programs are developed on XP, but run on Windows 10. This makes it impossible for me to debug JavaScript while developing.

    I know that simply replacing let with var is not a good idea, and I don't plan to do it. I'm just using this example to test the speed of different regular expression engines.

  14. #14
    Frenzied Member PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Newport, UK
    Posts
    1,194

    Re: How to remove all extra spaces, blank lines, and CrLfs in JavaScript code?

    Quote Originally Posted by dreammanor View Post
    Yes, my XP development environment does not support ECMAScript 2015. All my programs are developed on XP, but run on Windows 10. This makes it impossible for me to debug JavaScript while developing.

    I know that simply replacing let with var is not a good idea, and I don't plan to do it. I'm just using this example to test the speed of different regular expression engines.
    Have you considered incorporating something like https://babeljs.io into your pipeline? It can handle transpiling between versions for you and it can also cope with minification etc. as well.

  15. #15

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2012
    Posts
    2,038

    Re: How to remove all extra spaces, blank lines, and CrLfs in JavaScript code?

    Quote Originally Posted by PlausiblyDamp View Post
    Have you considered incorporating something like https://babeljs.io into your pipeline? It can handle transpiling between versions for you and it can also cope with minification etc. as well.
    I haven't tested Babel on XP, but I checked the source code of Babel and it seems to use more ECMAScript 2015 syntax than UglifyJS. I guess the new version of Babel should not run on XP. Unless I look for and try those very old versions of Babel.

  16. #16

    Re: How to remove all extra spaces, blank lines, and CrLfs in JavaScript code?

    using this site that is remove all unnecessary code

    http://www.odditysoftware.com/page-webtools16.htm

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width