正则匹配单行注释问题

如何匹配js中的单行注释?

我有一段字符串

var str = `
 //test 1
console.log('123'); //test 2
console.log('http://a.com')`;

str.match(/\/\/[^\n]*/g);

想匹配出//test 1 //test 2这种注释,而且排除掉http://a.com,该如何写正则呢

阅读 3.4k
1 个回答
/^\/\/\S*/g

/(?<!\:)\/\/\S*/g 排除掉 //前有:的情况


/(?<!\:)\/\/[^\n]*/g //匹配更完全
推荐问题