Javascipt matches regular expressions for single and multi line comments

  • 2020-03-29 23:53:29
  • OfStack

When using node.js. If we use a.json file to store some configuration, we want to add some comments.

However, since the string is read and converted to JSON objects by json.parse, the annotation cannot be converted correctly or even an error is reported.

The following regular expression matches all comments in the string, including single - and multi-line comments

Test address: 30 JRH
Note that you should use the escape character \ when using it as a regular for strings

So I need to write it like this:
var reg = "(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)|(//.*)"; 
var exp = new RegExp(reg,"g"); 

Use \\\ for \

Related articles: