javascript - 正则表达式匹配获取指定内容
本文介绍了javascript - 正则表达式匹配获取指定内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
有一个字符串
var str = '//@(test/js/index.js); //@(tass/js/inde.js); //@(tab/jsx/ind.jsx); 11230120 ;// var a = 22';';
我用 var reg = /(\/\/\@\()(\S*?)(\))/g ;
结果是这样的
有没有一个正则匹配的规则;能获取到这样的结果
['test/js/index.js','tass/js/inde.js','tab/jsx/ind.jsx]
解决方案
/(?:@\()([^)]*)/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}`);
});
}
这篇关于javascript - 正则表达式匹配获取指定内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文