Regex for comments in lines, lines in comments, etc.

This is a question that I decided and wanted to post in Q & A style, because I think more people can use this solution. Or maybe improve the solution, show where it breaks.

Problem

You want to do something with quoted lines and / or comments in the text. You want to extract them, highlight what you have. But some quoted lines are inside comments, and sometimes comment characters are inside lines. And line separators can be escaped, and comments can be line comments or block comments. And when you thought you had a solution, someone complains that it doesn't work when there is a regular expression in its JavaScript. What to do?

Specific example

var ret = row.match(/'([^']+)'/i); // Get 1st single quoted string content if (!ret) return ''; /* return if there no matches Otherwise turn into xml: */ var message = '\t<' + ret[1].replace(/\[1]/g, '').replace(/\/@(\w+)/i, ' $1=""') + '></' + ret[1].match(/[A-Z_]\w*/i)[0] + '>'; alert('xml: \'' + message + '\''); /* alert("xml: '" + message + "'"); // */ var line = prompt('How do line-comments start? (eg //)', '//'); // do something with line 

This code is nonsense, but how can I do the right thing in each case of the above JavaScript?

The only thing I found that this is close is the following: Comments in lines and lines in comments , where Jan Goewaers himself answered in the same way. But this still does not cope with the acceleration of the apostrophe.

+1
source share
1 answer

I broke the regex into 4 lines corresponding to 4 paths on the chart, don't leave these line breaks there if you ever use this.

 (['"])(?:(?!\1|\\).|\\.)*\1| \/(?![*/])(?:[^\\/]|\\.)+\/[igm]*| \/\/[^\n]*(?:\n|$)| \/\*(?:[^*]|\*(?!\/))*\*\/ 

Regular expression visualization

Demo version of Debuggex

This code captures 4 types of "blocks" that others may contain. 3. You can go through it and do with whatever you want, or discard it, because it is not the one you want to do something on.

This is one for JavaScript, as it is a language I am familiar with. But you can easily adapt this to the language of your preferences.

Does anyone see the way this code breaks?

Change Since then I have been informed that the general template is described very well here: fooobar.com/questions/5667 / ... , neato!

+2
source

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


All Articles