Reverberations

News, Views, Rants and Raves About Technology and More

Internet Explorer’s Trailing Comma Woes

with 8 comments

Internet Explorer is notorious for breaking on trailing commas in JavaScript array declaration. e.g.

var obj = {
     a: 1,
      b: 2,
};

fails on IE, while all other browsers just ignore the innocuous trailing comma after second element.

Weeding out these commas from JavaScript code is absolute PITA. However, here is a regular expression search string I wrote to search such instances in the code.

,\s*\n+\s*[\}\)\]]

Even better,
,\s*\n+(\s*\/\/.*\n)*\s*[\}\)\]]
matches multiple new lines and comments.

About these ads

Written by Brajesh

March 18, 2008 at 10:31 pm

8 Responses

Subscribe to comments with RSS.

  1. That’s actually an object literal, not an array.

    I just came across your page because I’ve noticed that this script:

    alert([1, 2, 3, ].length);

    displays “4” in IE 8, but “3” in Firefox and Safari.

    earwicker

    March 3, 2010 at 9:00 pm

    • As o [1,2,3,].length and [1,2,3,,,,].length, I really do not know which behaviour is correct according to the spec.

      In rhino i have this:

      js> [1,2,3].length
      3
      js> [1,2,3,].length
      3
      js> [1,2,3,,].length
      4
      js> [1,2,3,,,].length
      5
      js> [1,2,3,,,,].length
      3
      js> [1,2,3,,,,,].length
      3
      js> [1,2,3,,,,,,].length
      3

      Very strange. VERY STRANGE.

      witek

      July 24, 2010 at 4:56 am

      • as earwicker said “That’s actually an object literal, not an array” … so this new change applies to object literals only. not to arrays. For arrays trailing comma is still illeagle

        aaaaaa

        June 6, 2013 at 8:42 pm

  2. [...] fly in IE7, so remove the extra comma. If you need a way to do this in bulk, you could try regex. The corrected version looks like: <script type="text/javascript"> function [...]

  3. Can you pls make me an easy example to use your regex to check a full external js code? Thanks in advance. ciao h.

    haltman

    May 2, 2011 at 4:48 pm

  4. Nice, thanks for posting that.

    Travis

    September 17, 2012 at 3:40 pm

  5. To use this regexp in command line, do the following:

    find -name ‘*.js’ -exec grep -Pzl “,\s*\n+(\s*\/\/.*\n)*\s*[\}\)\]]” {} \;

    drachenfels

    November 28, 2012 at 6:13 pm

  6. I hope you never drop this page, I consistently forget the regex to find trailing commas in javascript and quickly search the web for ‘ie trailing comma regex’ to hopefully find my way back to your post. kudos for publishing this, it’s been a savior for my teams! Thanks again!!!

    deanpoulin

    September 12, 2013 at 4:05 am


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: