Returning lines for a given function from a JavaScript file The function below iterates through an array that contains all the lines of a JavaScript file (a global array, this_file), and returns just those lines (current_function) containing the text of the function with the name that is passed by the argument (funShun). The code assumes that the function to copy ends whenever '}' by itself (not preceded by whitespace) is spotted (this is true of the file I'm working with).

My question is about whether it would be preferable (and how to do it) to substitute a regex in place of iterating through the file line by line.

This works fine, but it seems to me this could be done with a regex applied to the original Ajax string, but that might be beyond my current regex abilities.

All advice greatly welcome!

// Return an array containing the text of the input function.
function load_function_text(funShun) {
  // Iterate through the present JS file's text.
  var line_num = 0; // Track line num so you can grab the previous line.
  var current_function = [];
  var copying_lines = false;
  this_file.forEach(function(line) {
    // Start copying when 'function funShun' is spotted. (Start pushing from previous line.)
    if (line.match("function " + funShun)) {
      current_function.push(this_file[line_num - 1]);
      current_function.push(line);
      copying_lines = true;
      line_num += 1;
    // Stop copying when '}' is spotted.
    } else if (copying_lines &amp;&amp; line === '}') {
      // Add the '}' line.
      current_function.push(line);
      // Stop copying when end of function is reached.
      copying_lines = false;
    } else {
      if (copying_lines) current_function.push(line);
      line_num += 1;
    }
  });
  return current_function;
}
Inefficient Array.forEach

The current implementation will needlessly iterate until the end of the array even after the function was found.
It would be better replace the .forEach with a regular counting loop,
and break out of it after you found the end of the function.

Avoid flag variables

Using a single loop and the copying_lines flag variable to alternate between processing logic (find start of function, copy content, find end), is not very easy to read.
It would be easier to use a nested loop after you found the start.
Something like this:

for (let i = 0; i &lt; lines.length; i++) {
  if (lines[i].match(startPattern) !== null) {
    let function_lines = [];

    for (let j = i; j &lt; lines.length; j++) {
      function_lines.push(lines[j]);

      if (lines[j] === '}') return function_lines;
    }

    throw new Error('Could not find end of function before end of content');
  }
}

return [];


Too weak start pattern

This condition is too weak to match the declaration of a function:


if (line.match("function " + funShun)) {



For example, if there are functions named convert_custom and convert,
and you call load_function_text('convert'),
it will find the first one.
I suggest to use a stronger pattern, for example:

startPattern = new RegExp("\\s*function " + funShun + "\\s*\\(");