Please help with an expression that throws a Fatal error...

Discussion in 'JavaScript' started by kavakava, Sep 27, 2008.

  1. #1
    Hello,

    I am trying to use Minify to dynamically minify and compress some javascripts. However, one of them causes a Fatal error, and I have pinpointed the problem to this regular expression, and I even know which symbol is causing it - but I really know nothing about Javascript, so could really use some help.

    The code throwing the error is:

    return Validation.get('IsEmpty').test(v) || /^[a-z0-9,!\#\$%&'\*\+/=\?\^_`\{\|}~-]+(\.[a-z0-9,!#\$%&'\*\+/=\?\^_`\{\|}~-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*\.([a-z]{2,})/i.test(v)
    }],

    And the symbol causing all the problems is the ' after the second a-z0-9, after !#\$%& and just before the \

    Now - why does this work on its own, but goes crazy if I try to minify it? Can I adjust something to make it play nice?

    Many thanks!
     
    kavakava, Sep 27, 2008 IP
  2. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #2
    is ' even allowed in an email address? i know that's not helpful. what do you use? jsmin or yui compressor or the dean edwards packer? if it's not something that relies on EVAL, then i really don't know. yui one has options such as "Minify only, no symbol obfuscation", could be relevant here.

    to be honest, minifying the script is probably not needed - just enable gzip compression for .js server side - it beats any minifying you can ever do.

    good luck
     
    dimitar christoff, Sep 28, 2008 IP
  3. kavakava

    kavakava Well-Known Member

    Messages:
    258
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    131
    #3
    Hi,

    Many thanks for your reply.

    I'm actually using Minify:
    http://code.google.com/p/minify/

    Minify combines all the JS dynamically and gzips as well - but the code I described causes a fatal error when testing and is not executing well. If I remove that ' it works fine - but I'm not sure if that will cause any problems or mess something else up.

    Many thanks!
     
    kavakava, Sep 28, 2008 IP
  4. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #4
    well, i am not sure about minify, but it's possible this is an error they need to be made aware of.

    essentially, you have regex that checks the bits before the @ sign of an email address against allowed characters there (I assume as per email RFC). however, it has 2 ' (in case they have name'.name2'@ ) - jsMin probably thinks that in the context of regex pattern definition, the bit between the single quotes is a string... you can try this:

    <script type="text/javascript">
    var testEmail = function(what) {
    return(/^[a-z0-9,!\#\$%&\'\*\+/=\?\^_`\{\|}~-]+(\.[a-z0-9,!#\$%&\'\*\+/=\?\^_`\{\|}~-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*\.([a-z]{2,})/i.test(what));
    }
    
    alert(testEmail("muad.dib@gmail.com")); // alerts true, ok
    alert(testEmail("paul.muad'dib@gmail.com")); // alerts true, ok
    
    </script>
    Code (markup):
    the difference is a \ infront of the ', so it becomes '\. in straight (non-jsmin) execution, it works fine as demostrated by the script above.

    try changing the test pattern like so and seeing if it works.
     
    dimitar christoff, Sep 28, 2008 IP
  5. kavakava

    kavakava Well-Known Member

    Messages:
    258
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    131
    #5
    Hi there,

    Many thanks for your reply. I tried adding the \ in front, but that did not help. It still gives the same error:

    "Uncaught exception 'JSMinException' with message 'Unterminated string literal.'"

    However, if I just remove the ' it all works fine, so it becomes:

    return Validation.get('IsEmpty').test(v) || /^[a-z0-9,!\#\$%&'\*\+/=\?\^_`\{\|}~-]+(\.[a-z0-9,!#\$%&\*\+/=\?\^_`\{\|}~-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*\.([a-z]{2,})/i.test(v)
                }],
    Code (markup):
    Does that look ok? Can I just remove the ' symbol?
    Many thanks!
     
    kavakava, Sep 28, 2008 IP
  6. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #6
    certainly so. it won't like the email address in the example for paul.muad'dib@ though.
     
    dimitar christoff, Sep 28, 2008 IP
  7. kavakava

    kavakava Well-Known Member

    Messages:
    258
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    131
    #7
    Hi Dimitar,

    Ok - I can live with that.

    Many thanks for your help, that is much appreciated!
    Thank you once again!
     
    kavakava, Sep 28, 2008 IP