Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Dec 2004
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    RegEx unexpected quantifier + unterminated characted class errors

    Hi. I'm getting the following RegEx errors:

    in IE 6:

    "unexpected quantifier"

    in Firefox 1.0:

    "unterminated characted class \"

    in Navigator 7:

    "unterminated characted class ["

    The code line at issue is:

    String.prototype.compact = function ()
    {
    return this.replace(/([.\'\"\w)\]+-])[\r\n]+/g,'$1;').replace(/;\{/g,'{').replace(/\s*([\[\]{};(),;&|!?:<>=*/%+-])\s*/g,'$1').replace(/(function\([^)]*\)\{[^}]+\})/g,'$1;').replace(/\/\*/g,'\/\*\n').replace(/;\*\//g,';\n\*\/\n').replace(/\/\*\n\*\//g,'\n\/\*\n\*/').replace(/\*\/\s*\/\*\s/g,'\n');
    };

    Firefox's JavaScript Console Arrow ends at/points to:

    replace(/([.\'\"\w)\]+-])[\r\n]+/g,'$1;').replace(/;\{/g,'{').replace(/\

    While Netscape's JavaScript Console Arrow ends at/points to:

    replace(/([.\'\"\w)\]+-])[\r\n]+/g,'$1;').replace(/;\{

    What I'm trying to do is compact a script by removing most spaces, returns, tabs, etc.

    It works perfectly on IE 5.16 for Mac which uses an older JScript engine.

    TIA

    Joseph

    REFERENCES

    http://www.google.com/search?hl=en&l...%22+javascript

    http://www.google.com/search?hl=en&l...%22+javascript

    http://msdn.microsoft.com/library/de...quantifier.asp

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    This is probably your problem cause:

    /\s*([\[\]{};(),;&|!?:<>=*/%+-])\s*/g

    Try escaping that one.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #3
    New to the CF scene
    Join Date
    Dec 2004
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by liorean
    This is probably your problem cause:

    /\s*([\[\]{};(),;&|!?:<>=*/%+-])\s*/g

    Try escaping that one.
    Thanks, liorean, for your suggestion + those related reference links, which I shall definitely research further.

    I was under the (false?) impression that the nice thing about [] is that you don't need to escape any character inside them except of course [ and ].

    Anyway, I tried your escape suggestion:

    /\s*([\[\]{};(),;&|!?:<>=*\/%+-])\s*/g

    and now IE 6 still gives:

    "unexpected quantifier"

    But now Firefox's JavaScript Console Arrow ends at/points to:

    "Error: unterminated character class &"

    replace(/\b

    While Netscape's JavaScript Console Arrow ends at/points to:

    "Error: unterminated character class ["

    replace(/\b

    which line appears in the opposite prototype String function:

    String.prototype.expand = function ()
    {
    return this.replace(/([{};])/g,'$1\n').replace(/\)\{/g,'\)\n\{').replace(/\)(\w)/g,'\) $1').replace(/(do|else|\))\{/g,'$1\n{').replace(/(if|for|switch|until|while|with|=)\(/g,'$1 (').replace(/\b(['")\]])?([&|!<>=*/+-]?)([?:&|=%*/+-])([!'"(\[])?\b/g,'$1 $2$3 $4').replace(/([\w'")\]])([*/%=<>?+-])([\w'"(\[])/g,'$1 $2 $3').replace(/(\w)(= \()/g,'$1 $2').replace(/([)\]])(=)/g,'$1 $2').replace(/,/g,', ').replace(/(for .+)\n(.+)\n/g,'$1 $2 ').replace(/;\s+([)'"])/g,';$1').replace(/([;}])\s+function/g,'$1\n\nfunction').replace(/\*\/\s*/g,'\*\/\n\n').replace(/\/\*\*\//g,'\n\/\*\n\*\/');
    };

    TIA

    Joseph

  • #4
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Quote Originally Posted by joemacman
    Thanks, liorean, for your suggestion + those related reference links, which I shall definitely research further.
    Just my regular sig. You might find my regex article handy, though.
    I was under the (false?) impression that the nice thing about [] is that you don't need to escape any character inside them except of course [ and ].
    And '/', '-', '^'. Obviously, since '/' is the delimiter for regex literals it must be escaped in all cases within the regex, including inside character sets. '-' specifies a range and is only treated as a literal at the end or beginning of a character set. '^' is treated as negation of the character set if appearing at the beginning. Also, I believe '[' does not require escaping in a character set, only ']' does.
    Anyway, I tried your escape suggestion /.../ and now IE 6 still gives:

    "unexpected quantifier"
    My tip here would be to first of all get the Microsoft Script Debugger. Second, it's likely that iew gets the character set delimiters wrong. You'll have to experiment.
    But now Firefox's JavaScript Console Arrow ends at/points to /.../ While Netscape's JavaScript Console Arrow ends at/points to /.../ which line appears in the opposite prototype String function:
    I would guess you have the same problem here... after looking you seem to have it multiple times, with the locations:

    /\b(['")\]])?([&|!<>=*/+-]?)([?:&|=%*/+-])([!'"(\[])?\b/g
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #5
    New to the CF scene
    Join Date
    Dec 2004
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yes, bravo, I already downloaded and added your tutorials to my reference library.

    Thanks also for the lesson on []. I've proceeded to unescape ' and " inside the []. My original code did place the - last inside the [] to distinguish it from being a range; and my original did place the ^ first inside the [] when I meant negation.

    It wasn't obvious to me / needs to be escaped, since other special characters like * and + don't need to be escaped inside the [].

    The good news is that your escape suggestions threw no errors in both the Firefox and Netscape JavaScript Consoles.

    However, while the String compaction + expansion appear to work properly in Firefox, the String expansion failed to work properly in Netscape (i.e., expansion of the compacted string did not reproduce the original string).

    Mozilla.org must be using different script engines for the 2 browsers???

    At least IE 6 though was consistent: it threw the same "unexpected quantifier" error. The MS Scripting Debugger unfortunately isn't very helpful.

    I'll test this when I get home:

    PHP Code:
    String.prototype.compact = function()
    {
    return 
    this.replace(/([.'"\w)\]+-])[\r\n]+/g,'$1;').replace(/;\{/g,'{').replace(/\s*([\[\]{};(),;&|!?:<>=*\/%+-])\s*/g,'$1').replace(/(function\([^)]*\)\{[^}]+\})/g,'$1;').replace(/\/\*/g,'/*n').replace(/;\*\//g,';n*/n').replace(/\/\*\n\*\//g,'n/*n*/').replace(/\*\/\s*\/\*\s/g,'n');
    };

    String.prototype.expand = function ()
    {
    return this.replace(/([{};])/g,'
    $1n').replace(/\)\{/g,')n{').replace(/\)(\w)/g,') $1').replace(/(do|else|\))\{/g,'$1n{').replace(/(if|for|switch|until|while|with|=)\(/g,'$(').replace(/\b(['")\]])?([&|!<>=*\/+-]?)([?:&|=%*\/+-])([!'"([])?b/g,'$1 $2$3 $4').replace(/([w'")\]])([*\/%=<>?+-])([\w'"(\[])/g,'$1 $2 $3').replace(/(\w)(= \()/g,'$1 $2').replace(/([)\]])(=)/g,'$1 $2').replace(/,/g,', ').replace(/(for .+)\n(.+)\n/g,'$1 $2 ').replace(/;\s+([)'"])/g,';$1').replace(/([;}])s+function/g,'$1\n\nfunction').replace(/*/s*/g,'\*\/\n\n').replace(//**//g,'\n\/\*\n\*\/');
    }; 
    I'll also try:

    PHP Code:
    String.prototype.expand = function ()
    {
    return 
    this.replace(/([{};])/g,'$1\n');
    };

    String.prototype.expand = function ()
    {
    return 
    this.replace(/([{};])/g,'$1\n').replace(/){/g,'\)\n\{');
    };

    String.prototype.expand = function ()
    {
    return 
    this.replace(/([{};])/g,'$1\n').replace(/){/g,'\)\n\{').replace(/)(w)/g,'\) $1');
    }; 
    etc., to narrow down which line is the problem.

    Joseph
    Last edited by joemacman; 12-16-2004 at 09:50 PM.

  • #6
    New to the CF scene
    Join Date
    Dec 2004
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Just to update: everything now works perfectly in IE6, Firefox 1, and Netscape 7, thanks to liorean's suggestions.

    The reason IE6 was throwing the "unexpected quantifier" error, however, was because I had the copyright © symbol (Unicode 00A9) in my comments, and IE6 replaced it with the unknown character symbol (?), hence RegEx thought it was an "unexpected quantifier."

    As for Netscape erroneously inserting $4 (the literal string and not its value), the problem went away when I placed the quantifier ? inside the pattern delimiters ():

    PHP Code:

    /*
    insert spaces before and after:
    &&, ||, '', "", >=, <=, *=, /=, +=, -=, etc.
    */

    String.prototype.expand = function ()
    {
    return 
    this.replace(/b(['")\]]?)([&|!<>=*\/+-]?)([?:&|=%*\/+-])([!'"(\[]?)\b/g,'$1 $2$3 $4')

    Whether this is formally correct RegEx, I really don't know, but so far it works for me.

    Joseph


  •  

    Posting Permissions

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