Regex for lines containing one string and not containing another string

后端 未结 1 1625
予麋鹿
予麋鹿 2021-01-28 23:46

I have following regex handy to match all the lines containing console.log() or alert() function in any javascript file opened in the editor supporting

相关标签:
1条回答
  • 2021-01-29 00:18

    You should use a negative lookhead, like this:

    ^(?!.*window\.).*\b(console\.log|alert)\b.*$
    

    The negative lookhead will assert that it is impossible to match if the string window. is present.

    Regex Demo

    As for the parenthesis, you can escape them with backslashes, but because you have a word boundary character, it will not match if you put the escaped parenthesis, because they are not word characters.

    The metacharacter \b is an anchor like the caret and the dollar sign. It matches at a position that is called a "word boundary". This match is zero-length.

    There are three different positions that qualify as word boundaries:

    • Before the first character in the string, if the first character is a word character.
    • After the last character in the string, if the last character is a word character.
    • Between two characters in the string, where one is a word character and the other is not a word character.
    0 讨论(0)
提交回复
热议问题