gpt4 book ai didi

javascript - 正则表达式查找功能模块主体的起始行和列

转载 作者:行者123 更新时间:2023-12-01 02:46:27 26 4
gpt4 key购买 nike

例如,我的代码是这样的:

    (function t(p1,p2,...) //comment
{
line 2,column 1

我认为正则表达式可以完成这项工作,但我不知道如何让正则表达式匹配结束以获得位置结果:第 2 行,第 1 列。

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

预先感谢您的帮助。

最佳答案

TLDR;

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

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

意思是:

  • 接受 ( 之前和之后的任何空格
  • 然后查找“function”字符串
  • 然后再次接受任何空白字符
  • 查找 ( 并直到 ) 字符或没有,循环字符
  • 然后在上一个循环结束后查找 ) 字符
  • 然后再次接受任何空白字符或不接受任何空白字符(也接受换行符)
  • 但在最后查找 { 字符。
<小时/>

这是我的 karma-code-reporter 的一部分 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;
}

取自 https://raw.githubusercontent.com/tpaksu/karma-code-reporter/master/index.js

关于javascript - 正则表达式查找功能模块主体的起始行和列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47228764/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com