Javascript 用于解析组的多个条件的正则表达式

Javascript 用于解析组的多个条件的正则表达式,javascript,regex,regex-lookarounds,regex-group,regex-greedy,Javascript,Regex,Regex Lookarounds,Regex Group,Regex Greedy,我试图用正则表达式将字符串分成3个不同的部分 我只能从字符串中获取函数参数,但我还想从字符串的其他部分获取函数参数 const regex = /(\(.*?\))+/g; const sampleString = 'collection.products(take:12|skip:16)'; const result = sampleString.match(regex) 它给了我(take:12 | skip:16) 但我还想获得收藏和产品 比赛预期结果 收藏 产品 take:12 | s

我试图用正则表达式将字符串分成3个不同的部分

我只能从字符串中获取函数参数,但我还想从字符串的其他部分获取函数参数

const regex = /(\(.*?\))+/g;
const sampleString = 'collection.products(take:12|skip:16)';
const result = sampleString.match(regex)
它给了我
(take:12 | skip:16)

但我还想获得
收藏
产品

比赛预期结果

  • 收藏

  • 产品

  • take:12 | skip:16


  • 您可以在
    (\(.*?\)+
    上拆分字符串,然后使用reduce以所需格式获取值

    const sampleString='collection.products(take:12 | skip:16)';
    const result=sampleString.split(/\.|)(\(.*?\)+/).reduce((op,inp)=>{
    if(inp){
    inp=inp.replace(/[)(]+/g',)
    op.push(inp)
    }
    返回操作
    },[])
    
    console.log(result)
    在这里,我们可以同时更改两个表达式:

    (\w+)|\((.+?)\)
    
    哪一组#1将捕获我们想要的单词
    (\w+)
    ,哪一组#2将捕获括号中想要的输出

    const regex=/(\w+)\(.+?)\)/gm;
    const str=`collection.products(take:12 | skip:16)`;
    让m;
    while((m=regex.exec(str))!==null){
    //这是避免具有零宽度匹配的无限循环所必需的
    if(m.index==regex.lastIndex){
    regex.lastIndex++;
    }
    //可以通过'm`-变量访问结果。
    m、 forEach((匹配,组索引)=>{
    log(`Found match,group${groupIndex}:${match}`);
    });
    
    }
    这将根据您的需要进行拆分

    const sampleString='collection.products(take:12 | skip:16)';
    const result=sampleString.split(/[.()]*([^.()]+)[.()]*/).filter(函数(el){return el!=“”;});
    
    console.log(result)
    如果字符串总是这样,请使用