简体   繁体   English

无痛方式删除JavaScript中的尾随逗号

[英]Pain-free way to remove trailing commas in javascript

IE (<9?) doesn't tolerate trailing commas at the end of object or list. IE(<9?)不能接受对象或列表末尾的逗号。 I know this too late, after developing for a few months in Chrome. 在Chrome中开发了几个月后,我知道为时已晚。

Now I have to search for every place I put in a trail comma, this is really painful. 现在,我必须搜索放在逗号后的每个位置,这确实很痛苦。 Is there any way(preferably automatic) to do this? 有什么办法(最好是自动的)来做到这一点? Like a editor plugin, or some script that search and replace these commas with blank? 像是编辑器插件,还是一些用空白搜索并替换这些逗号的脚本?

JSLint is an option, but it throws a lot of other warnings, and I have to paste in the scripts (which sometimes contain server-side template tags...). JSLint是一个选项,但是它会引发许多其他警告,我必须粘贴脚本(有时包含服务器端模板标签...)。

Some examples would have been good and which editor you use. 一些示例会很好,您将使用哪个编辑器。

Notepad++ and UltraEdit support both Perl regular expression replaces with back referencing. Notepad ++和UltraEdit支持Perl正则表达式替换为反向引用。

So you could try a Perl regular expression replace searching with the expression ,(\\s*?[)\\]]) and using \\1 as replace string. 因此,您可以尝试使用表达式,(\\s*?[)\\]])和使用\\1作为替换字符串来进行Perl正则表达式替换搜索。

This expression finds a comma before a closing round or square bracket with 0 or more spaces/tabs/line terminators between and keeps on replace everything except the comma. 此表达式在圆括号或方括号之间找到0或多个空格/制表符/行终止符之前的逗号,并继续替换除逗号以外的所有内容。

You should run this replace manually on your JavaScript code with checking what is found before making the replace. 您应该在执行替换之前,在JavaScript代码上手动运行此替换,并检查找到的内容。 And perhaps you need to run this replace several times in case of multiple commas at end of a list. 如果列表末尾有多个逗号,则可能需要多次运行此替换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM