RegEx用于匹配/替换JavaScript注释(多行和内联)

问题描述:

我需要使用JavaScript RegExp对象从JavaScript源代码中删除所有JavaScript注释。

I need to remove all JavaScript comments from a JavaScript source using the JavaScript RegExp object.

我需要的是RegExp的模式。

What I need is the pattern for the RegExp.

到目前为止,我发现这个:

So far, I've found this:

compressed = compressed.replace(/\/\*.+?\*\/|\/\/.*(?=[\n\r])/g, '');

此模式适用于:

/* I'm a comment */

/*
 * I'm a comment aswell
*/

但似乎不适用于内联:

// I'm an inline comment

我不是一个专家RegEx和它的模式,所以我需要帮助。

I'm not quite an expert for RegEx and it's patterns, so I need help.

此外,我想有一个RegEx模式,将删除所有这些类似HTML的注释。 p>

Also, I' would like to have a RegEx pattern witch would remove all those HTML-like comments.

<!-- HTML Comment //--> or <!-- HTML Comment -->

这些条件HTML注释也可以在各种JavaScript源代码中找到。

And also those conditional HTML comments witch can be found in various JavaScript sources.

感谢

试试这个,

(\/\*[\w\'\s\r\n\*]*\*\/)|(\/\/[\w\s\']*)|(\<![\-\-\s\w\>\/]*\>)

应该工作:)

should work :)