正则表达式,不能使用

sup*_*ipd 1 regex

我希望从源代码中提取类似于C的注释

(更新的例子)

/**
 * base comment
 * (c) SOMEBODY SOMETIME
 * something
 */

///<!-- ------metadata-XML------- -->
/// <module type="javascript"> A
///<desc> some desc 
///      </desc> 
(function( a /* param A */) { // programmers comment ... enclosure
/*! user doc
 this module ....
 * reguired
.....
*/
var b={}; // programmers in line comment
// single line comments

// The cookie spec says up to 4k per cookie, so at ~50 bytes per entry
// that gives a maximum of around 80 items as a max value for this field
    b.a=a;
    var str = " tttt \/\/this is not comment ! tttt "
    var str2 = " tttt \/\* this is not comment too ! \
.............. \*\/ ttt ";
    global.b = b; 
}(global);
///</module>
Run Code Online (Sandbox Code Playgroud)

我用的正则表达式是

^\s*\/\*(.*[\r\n]*)*\*\/
Run Code Online (Sandbox Code Playgroud)

问题是这个正则表达式停止(杀死)正则表达式引擎.RegexCouch变得不负责任,在浏览器中使用会导致不负责任的页面.

这个正则表达式有什么问题?怎么可能,regexp引擎无法解决它?是否存在一些无法使用的正则表达式(语法正确,我认为)?

ste*_*ema 5

这被称为灾难性回溯.您的正则表达式必须检查许多可能性,因为您正在嵌套量词:

^\s*\/\*(.*[\r\n]*)*\*\/
         ^^      ^ ^
Run Code Online (Sandbox Code Playgroud)

更好的方法是:

/^\s*\/\*.*?\*\//gms
Run Code Online (Sandbox Code Playgroud)

在这里看到它在行动.

您需要s选项将.匹配作为换行符,将匹配作为行的开头的m选项^.

.*? 匹配尽可能少的字符.