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?

3
  • 3
    Why not extract it from the AST using acorn or similar? Commented May 2, 2021 at 15:30
  • Thats new territory for me. Do you mean this? Commented May 2, 2021 at 15:38
  • 1
    A tiny, fast JavaScript parser, written completely in JavaScript. - github.com/acornjs/acorn. Try parsing a document using acorn and then log it. Commented May 2, 2021 at 15:41

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.