正则表达式匹配获取指定内容

有一个字符串

var str = '//@(test/js/index.js); //@(tass/js/inde.js); //@(tab/jsx/ind.jsx); 11230120 ;// var a = 22';';

我用 var reg = /(\/\/\@\()(\S*?)(\))/g ;
结果是这样的

https://img1.mukewang.com/5bfb9b180001194703390038.jpg

有没有一个正则匹配的规则;能获取到这样的结果

['test/js/index.js','tass/js/inde.js','tab/jsx/ind.jsx]


HUH函数
浏览 722回答 1
1回答

人到中年有点甜

/(?:@\()([^)]*)/g 测试地址const regex = /(?:@\()([^)]*)/g;const str = `//@(test/js/index.js); //@(tass/js/inde.js); //@(tab/jsx/ind.jsx); 11230120 ;// var a = 22';`;let m;while ((m = regex.exec(str)) !== null) {    // This is necessary to avoid infinite loops with zero-width matches    if (m.index === regex.lastIndex) {        regex.lastIndex++;    }        // The result can be accessed through the `m`-variable.    m.forEach((match, groupIndex) => {        console.log(`Found match, group ${groupIndex}: ${match}`);    });}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript