仅当字符串不以//开头时才选择正则表达式

时间:2017-03-04 18:55:34

标签: regex

我有这样的文字(代码)

function a() {
    console.log(1);

    // console.log(1.5) <-- skip this select only non commented
    console.log(2)
}

console.log('go')
console.info('test')

我想找到只使用正则表达式的注释console.logs 我试试这个https://regex101.com/r/gI6sN8/23,但它不适用于缩进字符串

正则表达式,以供将来参考:

^(?!(\/\/)\s)console\.(\w+).*

3 个答案:

答案 0 :(得分:1)

你的正则表达式很接近,在玩了一段时间后,我得到了一个似乎适用于你的例子的正则表达式。

https://regex101.com/r/gI6sN8/25

干杯。

这是我能想到的最好的正则表达式(我自己并不是很好)。它匹配的内容比它需要的多一点,但它永远不会匹配awk '{if ( $1 > 5 ) { {sum += $2; n++} END { if (n > 0) print sum / n; }}}' input.txt

的行

https://regex101.com/r/gI6sN8/29

答案 1 :(得分:1)

尝试使用以下正则表达式

(?<!\/\/\s)console.*?\);?

<强> DEMO

答案 2 :(得分:0)

试试这个正则表达式:https://regex101.com/r/gI6sN8/28

代码
\s*之前添加console以匹配具有缩进的字词。

^(?!\/\/)\s*console\.(\w+).*