regex - Detect and remove javascript object literal trailing commas - Stack Overflow

Trailing mas in JavaScript object literals are not accepted as a valid JavaScript syntax by IE7 :var a

Trailing mas in JavaScript object literals are not accepted as a valid JavaScript syntax by IE7 :

var a = {
  foo: 12,
  bar: 13,//this is ok in all browsers except ie7
};

For the moment the way I deal with this issue is to open my website using IE7 and use the console to find the invalid js files.

Do you know how to locate (or even better remove) trailing mas in a javascript file using UNIX mand line ?

I tried to grep these mas but a multiline regex is needed. I also googled the subject and found nothing useful.

Trailing mas in JavaScript object literals are not accepted as a valid JavaScript syntax by IE7 :

var a = {
  foo: 12,
  bar: 13,//this is ok in all browsers except ie7
};

For the moment the way I deal with this issue is to open my website using IE7 and use the console to find the invalid js files.

Do you know how to locate (or even better remove) trailing mas in a javascript file using UNIX mand line ?

I tried to grep these mas but a multiline regex is needed. I also googled the subject and found nothing useful.

Share Improve this question edited Jun 3, 2015 at 8:29 olivieradam666 asked Jul 3, 2012 at 10:46 olivieradam666olivieradam666 4,6702 gold badges21 silver badges26 bronze badges 1
  • @antyrat Added a paragraph to my question – olivieradam666 Commented Jul 3, 2012 at 10:50
Add a ment  | 

3 Answers 3

Reset to default 4

JSHint can find those for you. You can download it and run it from the mand line. It also has lots of useful additional things it can check for you (and options to turn the ones off that you don't want).

You can write a script that will do this for you:

$ cat 1.pl 
local $/;

my $data = <>;
$data =~ s@,(\s*)((?://.*?\n)?)(\s*)}@ $1$2$3}@msg;
print $data;

$ cat 1.txt 
var a = {
  foo: 12,
  bar: 13, //this is ok in all browsers except ie7
};

var b = {
  foo: 14,
  bar: 15,
};

$ perl -i 1.pl 1.txt

$ cat 1.txt 
var a = {
  foo: 12,
  bar: 13  //this is ok in all browsers except ie7
};

var b = {
  foo: 14,
  bar: 15 
};

It's not perfect but it locates files (it's not printing lines tho):

find -name '*.js' -exec grep -Pzl ",\s*\n+(\s*\/\/.*\n)*\s*[\}\)\]]" {} \;

Answer is partially taken from this question:

Regex (grep) for multi-line search needed

PS

For sure there is one flaw like that:

// whatever is done next,
}

It will be reported as trailing a, while it's not.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745330176a4622841.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信