Home > Back-end >  Regex for finding javascript code constructs
Regex for finding javascript code constructs

Time:05-21

I'm developing a regex that matches var, let and const in javascript code except within strings and comments. I managed this:

regex101

But this only matches 2 of the test cases. I'm stuck here.

CodePudding user response:

this regex matches any var, let or const which is at the start of a line and only preceded by spaces:

^\s*(var|let|const)

explanation:

  • ^ assert is start of line
  • \s* any amount of space characters (zero till n)
  • (var|let|const) match any of the words var, let or const

downside of this regex, it matches the spaces as well, which might not be desired

  • Related