Consider the following string
cont x = 10;
function foo() {
return x;
// ;; end of function ;; //
/* here is a some text
here too */
}
function bar() {
return 10
}
return foo() + bar();
// ;;done;; //
/* yolo
yolo
*/
As you can see this string contains javascript and I need to know if this string has a return at the end. But that final return (which is optional) can be followed by 0 or comments and 0 or more \n's AND should not be a return inside a function. The only thing I can think of is to strip all the comments and then have some regexp to check if the last line contains a return. Something like this:
myJsStr
.replace(/\/\/[^\n]+/gm, ';') // remove comments which start with //
.replace(/(\/\*.[^*\\]+\n*?\*\/)/gm, '') // remote comments /* ... */
.replace(/\n\s{0,}/gm,';'). // remove returns -> single line
.replace(/;{1,}/g, ';') // remove duplications of ;
.match(/(return)[^;]+;?$/) // check the last part
Any improvements on this?