0

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?

Jeanluca Scaljeri
  • 26,343
  • 56
  • 205
  • 333

1 Answers1

0

Javascript does not have a regular grammar. So you can't use regular expressions to break a string of valid javascript down into it's meaningful tokens.

If you really want to understand the extend of what you can do with regular expressions, checkout the Chomsky Hierarchy and understand that regular expressions can only match the simplest, regular languages.

If you want to have a quick laugh, checkout this answer and search the web for a good javascript parser you can use. And go on with your life.

PS Or as an exercise, you can write a recursive descent parser yourself... And you can even use regex in your implementation. But a finite list of applications of any regex, will not do the trick.

Chris Wesseling
  • 6,226
  • 2
  • 36
  • 72