How to comment on a block containing regexp

Do you know if there is a smart way to comment out a block of code containing regexp that contains * /?

For example, I found myself commenting on a block containing this statement:

... messageParser.run('messageFetched',/.*/); ... 

But if I comment on the block:

 /* messageParser.run('messageFetched',/.*/); */ 

then javascript interprets regexp as closing the comment and cannot parse the file. I tried to put // in front of the offending line, but this does not help, and I do not want to comment on every line in the block and not change the material of the regular expression.

Is there a smart way to do this?

+6
source share
3 answers

Change your regex to /(.*)/ :

 messageParser.run('messageFetched',/(.*)/); /* messageParser.run('messageFetched',/(.*)/); */ 
+2
source

My smart way to do this ...

Move your code to a function, for example:

 function mP () { messageParser.run('messageFetched',/.*/); } 

Then you can use

 /* mP(); */ 

Basically, this moves your regular expression to the side, so that you freely use block comments without changing the regular expression code.

+2
source

I know I'm late because the best answer has already been chosen, but since you were looking for a "smart trick", I would suggest ending up with an empty group that is not exciting:

 /.*(?:)/ 

which is equivalent to:

 /.*/ 

This is most convenient if you do not want to change the code. (for example, by refactoring or as a side effect of introducing a capture group.) It is also semantically the same (introducing a capture group would not be, and this could lead to the need to rewrite the logic) and has fewer negative effects on performance compared to the proxy function .

The official ECMAScript specification (which JavaScript is based on) actually recommends using the empty non-capturing group / (?:) / To represent an "empty" regular expression.

+1
source

Source: https://habr.com/ru/post/955848/


All Articles