Javascript 用regexp提取glsl函数体

Javascript 用regexp提取glsl函数体,javascript,regex,Javascript,Regex,我无法使用javascript在glsl脚本中提取函数 以下是glsl的内容: precision highp float; varying vec4 v_color; #if USE_TEXTURE varying vec2 v_uv0;

我无法使用javascript在glsl脚本中提取函数

以下是glsl的内容:

 precision highp float;                           
  varying vec4 v_color;                                 
  #if USE_TEXTURE                                       
      varying vec2 v_uv0;                               
      uniform sampler2D texture;                        
  #endif                                                
vec2 sfx_func_uv_to_01(vec4 ucrect, vec2 uv) {
    return vec2(
        sfx_func_uv_to_01(ucrect[0], ucrect[0] + ucrect[2], uv.x),
        1.0 - sfx_func_uv_to_01(ucrect[1], ucrect[1] - ucrect[3], uv.y)
    );
}
vec2 sfx_func_uv_to_01(vec4 ucrect, vec2 uv) {
    return vec2(1.0, 2.0);
}
vec4 sfx_fs_output_uv_quarter(vec2 uv, vec4 color) {
    vec4 result = color;
    return result;
}
 void main () {                                    
    vec2 uv = v_uv0;                                  
    vec4 color = texture2D(texture, uv);              
    gl_FragColor = color;
}                                             
我想用主体提取脚本中的4个函数。所以我尝试了注册:

/^(vec4|vec3|vec2|float|int).*\{.*\}/gms
js代码是:

for (const match of glsl1.matchAll(/^(vec4|vec3|vec2|float|int).*\{.*\}/gms)) {
    console.log(match[0]);
}
我只得到一个结果,而不是4个函数:

vec2 sfx_func_uv_to_01(vec4 ucrect, vec2 uv) {
    return vec2(
        sfx_func_uv_to_01(ucrect[0], ucrect[0] + ucrect[2], uv.x),
        1.0 - sfx_func_uv_to_01(ucrect[1], ucrect[1] - ucrect[3], uv.y)
    );
}
vec2 sfx_func_uv_to_01(vec4 ucrect, vec2 uv) {
    return vec2(1.0, 2.0);
}
vec4 sfx_fs_output_uv_quarter(vec2 uv, vec4 color) {
    vec4 result = color;
    return result;
}
 void main () {                                    
    vec2 uv = v_uv0;                                  
    vec4 color = texture2D(texture, uv);              
    gl_FragColor = color;
}    
我搜索了多行文本的非贪婪模式的解决方案,但失败了

任何建议都将不胜感激,谢谢:)

使用

/^(\s*)(vec4 | vec3 | vec2 | float | int | void)。*?{[\s\s]*?^\1}\s*$/gm

解释

--------------------------------------------------------------------------------
^字符串的开头
--------------------------------------------------------------------------------
(组和捕获到\1:
--------------------------------------------------------------------------------
\s*空格(\n、\r、\t、\f和“”)(0
或更多次(与最大金额匹配)
(可能的)
--------------------------------------------------------------------------------
)结束\1
--------------------------------------------------------------------------------
(分组并捕获到\2:
--------------------------------------------------------------------------------
vec4'vec4'
--------------------------------------------------------------------------------
|或
--------------------------------------------------------------------------------
vec3'vec3'
--------------------------------------------------------------------------------
|或
--------------------------------------------------------------------------------
vec2'vec2'
--------------------------------------------------------------------------------
|或
--------------------------------------------------------------------------------
浮动“浮动”
--------------------------------------------------------------------------------
|或
--------------------------------------------------------------------------------
int‘int’
--------------------------------------------------------------------------------
|或
--------------------------------------------------------------------------------
无效“无效”
--------------------------------------------------------------------------------
)结束\2
--------------------------------------------------------------------------------
.*?                      除\n以外的任何字符(0次或更多次
(匹配尽可能少的金额)
--------------------------------------------------------------------------------
{                        '{'
--------------------------------------------------------------------------------
[\s\s]*?任意字符:空白(\n\r\t,
\f、 和“”),非空白(除“\n、,
\r、 \t、\f和“”)(0次或多次
(匹配尽可能少的金额)
--------------------------------------------------------------------------------
^字符串的开头
--------------------------------------------------------------------------------
\1与捕获匹配的内容\1
--------------------------------------------------------------------------------
}                        '}'
--------------------------------------------------------------------------------
\s*空格(\n、\r、\t、\f和“”)(0或
更多次(与最多金额匹配)
(可能的)
--------------------------------------------------------------------------------
$在可选的前\n和
一串

对于您当前的情况,稍微修改一下就足够了-
^(vec4 | vec3 | vec2 | float | int)。*?{.*}
。参考-。但它不处理函数中存在
{}
的情况。模式会变得有点复杂,很抱歉我的回复太晚了。我试过正则表达式,它几乎是正确的。但是,当函数中有一个
}
时,函数体将在第一个
}
字符处中断。您可以看到以下示例:我试图解决此问题,但失败了。“你能帮我吗,谢谢。”Supersuraccon更新。