Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Regex查找函数模块主体的起始行和列_Javascript_Regex - Fatal编程技术网

Javascript Regex查找函数模块主体的起始行和列

Javascript Regex查找函数模块主体的起始行和列,javascript,regex,Javascript,Regex,例如,我的代码如下所示: (function t(p1,p2,...) //comment { line 2,column 1 我认为正则表达式可以完成这项工作,但我不知道如何获得正则表达式匹配的结尾以获得位置结果:第2行,第1列 我只需要找到匹配(.function.(.)的结束位置。{其中表示任何内容 提前感谢您的帮助。TLDR 这部分代码用于启动函数: var regex1 = /\s*\(\s*function\s*\([^)]*\)\s*\{/; 指: 接受前

例如,我的代码如下所示:

    (function t(p1,p2,...) //comment
      {
line 2,column 1
我认为正则表达式可以完成这项工作,但我不知道如何获得正则表达式匹配的结尾以获得位置结果:第2行,第1列

我只需要找到匹配
(.function.(.)的结束位置。{
其中
表示任何内容

提前感谢您的帮助。

TLDR

这部分代码用于启动函数:

var regex1 = /\s*\(\s*function\s*\([^)]*\)\s*\{/;
指:

  • 接受
    前后的任何空格(
  • 然后查找“函数”字符串
  • 然后再次接受任何空白字符
  • 查找
    ),直到
    字符或无,循环字符
  • 然后在上一个循环结束后查找
    字符
  • 然后再次接受任何空白字符或无(也接受换行符)
  • 但最后要寻找
    {
    字符

这是jasmine测试模块的一部分。我用它来解析jasmine定义文件。
content
是用换行符分割的文件内容数组

/**
   * calculates the jasmine formatted test file method bounds
   * @param {array} content: line by line splitted test code content
   * @returns {array} bounds of the methods separated by groups, specs and de-constructors.
   */
  this.findBounds = function (content) {
    // prepare the output object
    var bounds = { groups: [], cases: [], deconst: [] };
    // loop each line
    for (var i = 0; i < content.length; i++) {
      // prepare regular expressions for describe, it and before/afterEach blocks
      var regex1 = /describe\s*\(.+?,\s*function\s*\([^)]*\)\s*\{/;
      var regex2 = /it\s*\(.+?,\s*function\s*\(/;
      var regex3 = /(?:beforeEach|afterEach)\s*\(\s*function\s*\(/;
      // if it's a match for "describe"
      if ((regex1.exec(content[i]) || []).length > 0) {
        // push the bounds for this block
        bounds.groups.push([i, this.findEndLine(content, i)]);
      }
      // if it's a match for "it"
      else if ((regex2.exec(content[i]) || []).length > 0) {
        // push the bounds for this block
        bounds.cases.push([i, this.findEndLine(content, i)]);
      }
      // if it's a match for "beforeEach" or "afterEach"
      else if ((regex3.exec(content[i]) || []).length > 0) {
        // push the bounds for this block
        bounds.deconst.push([i, this.findEndLine(content, i)]);
      }
    }
    // return the calculated bounds
    return bounds;
  };

  /**
   * Finds the end bracket for a given start bracket on the content
   * @param {string} content    : line by line split test code content
   * @param {integer} startLine : the line number containing the start bracket
   */
  this.findEndLine = function (content, startLine) {
    // find the start bracket position
    var match = /\bfunction\s*\([^)]*\)\s*({)/i.exec(content[startLine]);
    var pos = match[0].length + match.index - 1;
    // prepare a stack for the brackets
    var $stack = [];
    // loop each character
    while (startLine < content.length) {
      while (pos < content[startLine].length) {
        // read the char
        var read = content[startLine][pos];
        // if read char is a start bracket
        if (read == "{") {
          // push it to the array
          $stack.push("{");
        } else if (read == "}") {
          // if read char is a closing bracket, read from the stack
          var val = $stack.pop();
          if (val == "{") {
            // if the closing bracket matches with an opening bracket
            if ($stack.length == 0) {
              // if there are no brackets left, return the end line
              return startLine;
            }
          } else {
            // the brackets are not correctly positioned, return error
            return -1;
          }
        }
        // go to the next char
        pos++;
      }
      // go to the next line
      startLine++;
      // reset position
      pos = 0;
    }
    // if this line is reached, there must be an error.
    return -1;
  }
/**
*计算jasmine格式的测试文件方法边界
*@param{array}content:逐行拆分的测试代码内容
*@返回由组、规范和反构造函数分隔的方法的{array}界。
*/
this.findBounds=函数(内容){
//准备输出对象
var界限={groups:[],cases:[],deconst:[]};
//循环每行
对于(变量i=0;i0){
//按此块的边界
bounds.groups.push([i,this.findEndLine(content,i)]);
}
//如果它与“它”匹配
else if((regex2.exec(content[i])| |[]).length>0){
//按此块的边界
bounds.cases.push([i,this.findEndLine(content,i)]);
}
//如果它与“beforeach”或“afterEach”匹配
else if((regex3.exec(content[i])| |[]).length>0){
//按此块的边界
bounds.deconst.push([i,this.findEndLine(content,i)]);
}
}
//返回计算出的边界
返回边界;
};
/**
*查找内容上给定开始括号的结束括号
*@param{string}content:逐行拆分测试代码内容
*@param{integer}行:包含起始括号的行号
*/
this.findEndLine=函数(内容,行){
//找到起始支架位置
var match=/\b函数\s*\([^)]*\)\s*({)/i.exec(内容[startLine]);
var pos=match[0]。长度+match.index-1;
//为括号准备一个堆栈
var$stack=[];
//循环每个字符
while(startine
取自TLDR

这部分代码用于启动函数:

var regex1 = /\s*\(\s*function\s*\([^)]*\)\s*\{/;
指:

  • 接受
    前后的任何空格(
  • 然后查找“函数”字符串
  • 然后再次接受任何空白字符
  • 查找
    ),直到
    字符或无,循环字符
  • 然后在上一个循环结束后查找
    字符
  • 然后再次接受任何空白字符或无(也接受换行符)
  • 但最后要寻找
    {
    字符

这是jasmine测试模块的一部分。我用它来解析jasmine定义文件。
content
是用换行符分割的文件内容数组

/**
   * calculates the jasmine formatted test file method bounds
   * @param {array} content: line by line splitted test code content
   * @returns {array} bounds of the methods separated by groups, specs and de-constructors.
   */
  this.findBounds = function (content) {
    // prepare the output object
    var bounds = { groups: [], cases: [], deconst: [] };
    // loop each line
    for (var i = 0; i < content.length; i++) {
      // prepare regular expressions for describe, it and before/afterEach blocks
      var regex1 = /describe\s*\(.+?,\s*function\s*\([^)]*\)\s*\{/;
      var regex2 = /it\s*\(.+?,\s*function\s*\(/;
      var regex3 = /(?:beforeEach|afterEach)\s*\(\s*function\s*\(/;
      // if it's a match for "describe"
      if ((regex1.exec(content[i]) || []).length > 0) {
        // push the bounds for this block
        bounds.groups.push([i, this.findEndLine(content, i)]);
      }
      // if it's a match for "it"
      else if ((regex2.exec(content[i]) || []).length > 0) {
        // push the bounds for this block
        bounds.cases.push([i, this.findEndLine(content, i)]);
      }
      // if it's a match for "beforeEach" or "afterEach"
      else if ((regex3.exec(content[i]) || []).length > 0) {
        // push the bounds for this block
        bounds.deconst.push([i, this.findEndLine(content, i)]);
      }
    }
    // return the calculated bounds
    return bounds;
  };

  /**
   * Finds the end bracket for a given start bracket on the content
   * @param {string} content    : line by line split test code content
   * @param {integer} startLine : the line number containing the start bracket
   */
  this.findEndLine = function (content, startLine) {
    // find the start bracket position
    var match = /\bfunction\s*\([^)]*\)\s*({)/i.exec(content[startLine]);
    var pos = match[0].length + match.index - 1;
    // prepare a stack for the brackets
    var $stack = [];
    // loop each character
    while (startLine < content.length) {
      while (pos < content[startLine].length) {
        // read the char
        var read = content[startLine][pos];
        // if read char is a start bracket
        if (read == "{") {
          // push it to the array
          $stack.push("{");
        } else if (read == "}") {
          // if read char is a closing bracket, read from the stack
          var val = $stack.pop();
          if (val == "{") {
            // if the closing bracket matches with an opening bracket
            if ($stack.length == 0) {
              // if there are no brackets left, return the end line
              return startLine;
            }
          } else {
            // the brackets are not correctly positioned, return error
            return -1;
          }
        }
        // go to the next char
        pos++;
      }
      // go to the next line
      startLine++;
      // reset position
      pos = 0;
    }
    // if this line is reached, there must be an error.
    return -1;
  }
/**
*计算jasmine格式的测试文件方法边界
*@param{array}content:逐行拆分的测试代码内容
*@返回由组、规范和反构造函数分隔的方法的{array}界。
*/
this.findBounds=函数(内容){
//准备输出对象
var界限={groups:[],cases:[],deconst:[]};
//循环每行
对于(变量i=0;i0){
//按此块的边界
bounds.groups.push([i,this.findEndLine(content,i)]);
}
//如果它与“它”匹配
else如果((regex2.exec(content[i])| |[]).length>0