Raynos Raynos - 3 years ago 223
Javascript Question

regular expression to extract function body

I have this regexp

var bodyRegExp = /function[\\s]+[(].+[)][\\s]+{(.+)}/;

bodyRegExp.exec("module.exports = function () { /* any content */ }");


It doesn't work. Why is it broken?

It's meant to pull the body of the function statement out of the source code.

Edit:

I'm being stupid. Trying to parse javascript with a regexp is stupid.

Answer Source

you cannot use regular expressions to parse JavaScript language syntax because the grammar for that language is too complex for what regex can do.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download