...

View Full Version : RegEx unexpected quantifier + unterminated characted class errors



joemacman
12-15-2004, 04:17 PM
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&lr=&q=%22unterminated+character+class%22+javascript

http://www.google.com/search?hl=en&lr=&q=%22unterminated+character+class%22+javascript

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/js56jserrunexpectedquantifier.asp

liorean
12-15-2004, 04:31 PM
This is probably your problem cause:

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

Try escaping that one.

joemacman
12-15-2004, 05:13 PM
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

liorean
12-15-2004, 06:35 PM
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

joemacman
12-16-2004, 07:29 PM
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:


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,'$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\*\/');
};

I'll also try:


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

joemacman
12-18-2004, 11:02 PM
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 ():




/*
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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum