从匹配字符串 (javascript) 的正则表达式中获取第一个匹配项及其索引位置的问题
Issue with getting the first matching item and its index position from a regex matching a string ( javascript)
我写了一个匹配字符串中所有函数名称的正则表达式
Regex :- /([a-zA-Z ]*(?=\())/g
String:- (( MAX(1,2,3,4), min(1,2,3), max(3,4,5)))
上面的正则表达式通过检查一堆单词后跟“(”来匹配所有函数名称。在这种情况下,匹配项是 MAX、MIN, MAX(除了我使用 match.filter(String) 过滤的一些空字符串。)
在我的一个条件下,我只需要“FIRST”匹配函数及其 START 和 STOP 索引。
所以,我写了下面的函数来获取它。
var re = /([a-zA-Z ]*(?=\())/g;
var str = "max(1,2), min(1,2)";
while ((match = re.exec(str)) !== null) {
console.log("match found at " + match.index);
// Pick the first matching index from here ?
}
但是这将进入无限循环并且它没有给出所需的输出(我确定上面的函数有问题,但不太确定是什么)。
Example string2 = (((( max(34234,234234,344) min(1,2,3)))))*23 + max(23434, 234234,234234))) - I only need the first matching function "max" from here along with it's start and stop index's.
以下解决方案需要您提供的模式(特别是函数前后的空格。)
.match()
方法提供了一个index
作为起始位置,使用字符串的.length
属性得到结束位置:
let string = '(((( max(34234,234234,344) min(1,2,3)))))*23 + max(23434, 234234,234234)))';
let result = '';
while (result != null) {
result = string.match(/\w+(?=\()/);
if (result != null) {
console.log('Found: ' + result, 'Start: ' + result.index, 'End: ' + parseInt(result[0].length + result.index));
}
string = string.split(result)[1];
}
要索引获取函数名和参数吗?如果是这样,则不需要索引,可以直接使用此正则表达式获取函数名称和参数:/([a-zA-Z]+) *\(([^\)]*)\)/g
:
const testCases = [
'max(1,2), min(1,2)',
'(( MAX(1,2,3,4), min (1,2,3), max(3,4,5)))'
];
let regex = /([a-zA-Z]+) *\(([^\)]*)\)/g;
let matches = testCases.forEach(str => {
console.log('input: ' + str);
let match = regex.exec(str);
while(match) {
console.log('- name: ' + match[1] + ', params: ' + match[2]);
match = regex.exec(str);
}
});
输出:
input: max(1,2), min(1,2)
- name: max, params: 1,2
- name: min, params: 1,2
input: (( MAX(1,2,3,4), min (1,2,3), max(3,4,5)))
- name: MAX, params: 1,2,3,4
- name: min, params: 1,2,3
- name: max, params: 3,4,5
此示例提取所有函数名称和参数对。如果您只对第一个感兴趣,请删除 while:
let matches = testCases.forEach(str => {
console.log('input: ' + str);
let match = regex.exec(str);
console.log('- name: ' + match[1] + ', params: ' + match[2]);
});
正则表达式的解释:
([a-zA-Z]+)
- 捕获组 1:函数名称是 1+ alpha
*
- 可选 space
\(
- 左括号
([^\)]*)
- 捕获组 2:直到右括号 之前的所有内容
\)
- 右括号
请注意,我将您的正则表达式从 ([a-zA-Z ]*)
更改为 ([a-zA-Z]+) *
以避免像 MA X (1,2)
.
这样的误报
我写了一个匹配字符串中所有函数名称的正则表达式
Regex :- /([a-zA-Z ]*(?=\())/g
String:- (( MAX(1,2,3,4), min(1,2,3), max(3,4,5)))
上面的正则表达式通过检查一堆单词后跟“(”来匹配所有函数名称。在这种情况下,匹配项是 MAX、MIN, MAX(除了我使用 match.filter(String) 过滤的一些空字符串。)
在我的一个条件下,我只需要“FIRST”匹配函数及其 START 和 STOP 索引。 所以,我写了下面的函数来获取它。
var re = /([a-zA-Z ]*(?=\())/g;
var str = "max(1,2), min(1,2)";
while ((match = re.exec(str)) !== null) {
console.log("match found at " + match.index);
// Pick the first matching index from here ?
}
但是这将进入无限循环并且它没有给出所需的输出(我确定上面的函数有问题,但不太确定是什么)。
Example string2 = (((( max(34234,234234,344) min(1,2,3)))))*23 + max(23434, 234234,234234))) - I only need the first matching function "max" from here along with it's start and stop index's.
以下解决方案需要您提供的模式(特别是函数前后的空格。)
.match()
方法提供了一个index
作为起始位置,使用字符串的.length
属性得到结束位置:
let string = '(((( max(34234,234234,344) min(1,2,3)))))*23 + max(23434, 234234,234234)))';
let result = '';
while (result != null) {
result = string.match(/\w+(?=\()/);
if (result != null) {
console.log('Found: ' + result, 'Start: ' + result.index, 'End: ' + parseInt(result[0].length + result.index));
}
string = string.split(result)[1];
}
要索引获取函数名和参数吗?如果是这样,则不需要索引,可以直接使用此正则表达式获取函数名称和参数:/([a-zA-Z]+) *\(([^\)]*)\)/g
:
const testCases = [
'max(1,2), min(1,2)',
'(( MAX(1,2,3,4), min (1,2,3), max(3,4,5)))'
];
let regex = /([a-zA-Z]+) *\(([^\)]*)\)/g;
let matches = testCases.forEach(str => {
console.log('input: ' + str);
let match = regex.exec(str);
while(match) {
console.log('- name: ' + match[1] + ', params: ' + match[2]);
match = regex.exec(str);
}
});
输出:
input: max(1,2), min(1,2)
- name: max, params: 1,2
- name: min, params: 1,2
input: (( MAX(1,2,3,4), min (1,2,3), max(3,4,5)))
- name: MAX, params: 1,2,3,4
- name: min, params: 1,2,3
- name: max, params: 3,4,5
此示例提取所有函数名称和参数对。如果您只对第一个感兴趣,请删除 while:
let matches = testCases.forEach(str => {
console.log('input: ' + str);
let match = regex.exec(str);
console.log('- name: ' + match[1] + ', params: ' + match[2]);
});
正则表达式的解释:
([a-zA-Z]+)
- 捕获组 1:函数名称是 1+ alpha*
- 可选 space\(
- 左括号([^\)]*)
- 捕获组 2:直到右括号 之前的所有内容
\)
- 右括号
请注意,我将您的正则表达式从 ([a-zA-Z ]*)
更改为 ([a-zA-Z]+) *
以避免像 MA X (1,2)
.