正则表达式 - JavaScript ,匹配函数第二个参数的正则表达式
Regex - JavaScript , match the regex for the 2nd argument of a function
我有一个名为 waitForCompletion(arg1,value,waitTime)
;
的函数
我想找到一种方法来知道我在函数的第二个参数上,即 waitForCompletion(arg1,
用例:- 当用户开始输入函数名称并使用第二个参数时,我必须触发一些智能感知以显示关于第二个参数可以接受的值的智能感知。
我目前有以下内容,但它只匹配函数名称并触发我对所有参数的智能感知,但它必须只对第二个参数执行此操作。
let completionMatch = textUntilPosition.trim().match(/(^waitForCompletion)|(\swaitForCompletion)/);
if(completionMatch){ //trigger intellisense }
我相信我们必须匹配函数名称后的 (
但不知何故忽略第一个参数并匹配然后匹配第一个参数后的 ,
,那时我可以确定我的模式匹配。
注意:- textUntilPosition 获取文本直到我的光标所在的位置,即如果我的光标在第二个参数 waitForCompletion(arg1,
中,如果我的光标在第一个参数上它是 waitForCompletion(arg1
When a user starts typing the function name and is on the second argument, I have to trigger some intellisense for showing the intellisense on what values the second argument can accept.
你不能用简单的正则表达式来做到这一点,你需要一个 JavaScript 解析器。有几个开源的可用,例如 Espree (the parser used by ESLint) and Acorn.
原因是 JavaScript 语法在“正则表达式”中的“正则”意义上不是 正则。例如,考虑:
waitForCompletion(x ? example(1, 2) : y,
你的正则表达式必须理解条件运算符 (? :
) 才能知道你当时在 waitForCompletion
的第二个参数中,而不是在向 waitForCompletion
提供第二个参数时更早example
。这只是参数可以比单个标识符或文字更复杂的众多方式之一。
我不想让人觉得我在阻挠这件事,或者看起来对此毫无帮助。这是一个示例,在许多情况下都可以使用您描述的简单类型的参数,但正如我在上面(以及下面的评论中)所说的那样,它 will 失败 lots 其他输入:
"use strict";
const rex = /(^|\s)waitForCompletion\s*\((?:[^,]+|(["'])[^]*?),\s*$/;
// true:
console.log(rex.test("waitForCompletion(arg1,"));
// false, wrong function:
console.log(rex.test("blahwaitForCompletion(arg1,"));
// false, second arg present
console.log(rex.test("waitForCompletion(arg1, arg2"));
// true:
console.log(rex.test("\twaitForCompletion(\"hi\","));
// true, there's a comma, but it's in a string (1):
console.log(rex.test("waitForCompletion(\"h,i\","));
// true, there's a comma, but it's in a string (2):
console.log(rex.test("waitForCompletion('h,i',"));
我有一个名为 waitForCompletion(arg1,value,waitTime)
;
我想找到一种方法来知道我在函数的第二个参数上,即 waitForCompletion(arg1,
用例:- 当用户开始输入函数名称并使用第二个参数时,我必须触发一些智能感知以显示关于第二个参数可以接受的值的智能感知。
我目前有以下内容,但它只匹配函数名称并触发我对所有参数的智能感知,但它必须只对第二个参数执行此操作。
let completionMatch = textUntilPosition.trim().match(/(^waitForCompletion)|(\swaitForCompletion)/);
if(completionMatch){ //trigger intellisense }
我相信我们必须匹配函数名称后的 (
但不知何故忽略第一个参数并匹配然后匹配第一个参数后的 ,
,那时我可以确定我的模式匹配。
注意:- textUntilPosition 获取文本直到我的光标所在的位置,即如果我的光标在第二个参数 waitForCompletion(arg1,
中,如果我的光标在第一个参数上它是 waitForCompletion(arg1
When a user starts typing the function name and is on the second argument, I have to trigger some intellisense for showing the intellisense on what values the second argument can accept.
你不能用简单的正则表达式来做到这一点,你需要一个 JavaScript 解析器。有几个开源的可用,例如 Espree (the parser used by ESLint) and Acorn.
原因是 JavaScript 语法在“正则表达式”中的“正则”意义上不是 正则。例如,考虑:
waitForCompletion(x ? example(1, 2) : y,
你的正则表达式必须理解条件运算符 (? :
) 才能知道你当时在 waitForCompletion
的第二个参数中,而不是在向 waitForCompletion
提供第二个参数时更早example
。这只是参数可以比单个标识符或文字更复杂的众多方式之一。
我不想让人觉得我在阻挠这件事,或者看起来对此毫无帮助。这是一个示例,在许多情况下都可以使用您描述的简单类型的参数,但正如我在上面(以及下面的评论中)所说的那样,它 will 失败 lots 其他输入:
"use strict";
const rex = /(^|\s)waitForCompletion\s*\((?:[^,]+|(["'])[^]*?),\s*$/;
// true:
console.log(rex.test("waitForCompletion(arg1,"));
// false, wrong function:
console.log(rex.test("blahwaitForCompletion(arg1,"));
// false, second arg present
console.log(rex.test("waitForCompletion(arg1, arg2"));
// true:
console.log(rex.test("\twaitForCompletion(\"hi\","));
// true, there's a comma, but it's in a string (1):
console.log(rex.test("waitForCompletion(\"h,i\","));
// true, there's a comma, but it's in a string (2):
console.log(rex.test("waitForCompletion('h,i',"));