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.
source share