正则表达式从 js 文件中删除所有函数 [Textmate 首选]

发布于 2024-09-07 09:57:51 字数 437 浏览 5 评论 0原文

我正在处理一个大且极其混乱的 JavaScript 文件,我想从该文件中删除所有函数,最终创建一个仅包含数据的版本。

代码看起来像这样:

var foo : bar = "hi";
function foobar (){
  //blah blah
}
var fobar:bar;
var barfo:bar;
function imSoUgly(){
  //Blah blah blah blah mr freeman
}

我想要构建的正则表达式会找到所有函数。{.} 并删除它们,产生这个:

var foo : bar = "hi";
var fobar:bar;
var barfo:bar;

我不太确定从哪里开始。理想情况下,我想使用 Textmate 的 RegEx 来完成此操作,但我很容易。

I'm working on a large and extremely messy javascript file, and I would like to remove all functions from the file, ultimately creating a version which contains only data.

the code looks something like this:

var foo : bar = "hi";
function foobar (){
  //blah blah
}
var fobar:bar;
var barfo:bar;
function imSoUgly(){
  //Blah blah blah blah mr freeman
}

The regex I would like to build would find all function.{.} and delete them, producing this:

var foo : bar = "hi";
var fobar:bar;
var barfo:bar;

I'm not quite sure where to start with this. Ideally I would like to do it with Textmate's RegEx, but I'm easy.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

美人迟暮 2024-09-14 09:57:51

我认为仅使用正则表达式不可能做到这一点,因为不可能匹配可以任意深度嵌套的开始和结束大括号(代码块)。

为了可靠地做到这一点,您需要递归地查看所有内部代码块以找到函数的末尾。或者类似的东西(计算大括号的数量,...)。

I don't think it is possible to do this with only with regular expressions, as it is not possible to match starting and ending braces (code blocks) which can be arbitrary deeply nested.

To do this reliably, you would need to recursively look through all the inner code code blocks to locate the end of the function. Or something like that (count the number of braces, ...).

╰つ倒转 2024-09-14 09:57:51

你不能。话虽如此,您可以使用类似的东西

function\s+\w+\s*\([^)]*\)\s*{[^}]*}

,但如果函数内有任何 {} ,它将失败,并且您对此无能为力

You can't. That being said you could use something like this

function\s+\w+\s*\([^)]*\)\s*{[^}]*}

but it will fail if there are any { or } inside the function and you can't do anything about this

攒一口袋星星 2024-09-14 09:57:51

在我看来,正则表达式不足以完成如此​​复杂的事情。我对正则表达式能做的最好的事情是:

[\r\n]function [\w ]*\(\)\{[\w\W]*?}

这将删除示例中的所有函数,但如果你有这样的东西,它就不起作用:

function foobar (){
   if(condition){
      // do something
   } // this end brace would be mis-interpreted as the end of the function
   // bla, bla, bla
}

你仍然会:

   // bla, bla, bla
}

悲观主义者的答案会起作用,但前提是所有的函数在结束行之前没有空格,这不太可能是真的。

底线是你确实需要一个真正的 JavaScript 解析器。 快速谷歌搜索发现了这个:

http://www.antlr.org/

In my opinion, Regex is not sufficient to do something as complex as this is. The best I could do with regex is this:

[\r\n]function [\w ]*\(\)\{[\w\W]*?}

That will remove all the functions in your example, but if you had something like this, it wouldn't work:

function foobar (){
   if(condition){
      // do something
   } // this end brace would be mis-interpreted as the end of the function
   // bla, bla, bla
}

You would still have:

   // bla, bla, bla
}

Pessimist's answer would work, but ONLY if all of the functions have no spaces before the closing line, which is unlikely to be true.

The bottom line is that you really need a real JavaScript parser. A quick google search found this:

http://www.antlr.org/

南薇 2024-09-14 09:57:51

您无法使用“常规”表达式来执行此操作,但某些语言提供模式匹配结构,允许您匹配(除其他外)平衡文本。

例如,Perl:

/function\s*\(\)\s*(\{([^{}]++|(?1))*\}/

它是否是完成这项工作的正确工具(提示:可能不是)完全是另一个问题。

You can't do this with a "regular" expression, but some languages provide pattern-matching constructs which allow you to match (among other things) balanced text.

For example, Perl:

/function\s*\(\)\s*(\{([^{}]++|(?1))*\}/

Whether it's the correct tool for the job (HINT: It probably isn't) is another question entirely.

属性 2024-09-14 09:57:51

-- 已删除 - Carko 是对的,正则表达式是解决该问题的一种非常幼稚的方法。
为此,您需要一个PEG

-- Deleted - Carko is right, a regexp would be a very naive approach to the problem.

You need a PEG for that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文