创建包含正则表达式匹配中所有组的数组的有效解决方案
Effective solution for create array containing all groups in regex matches
我正在寻找一种有效的方法来创建包含所有匹配项的数组包含正则表达式组匹配项。
例如正则表达式 /(1)(2)(3)/g
字符串 123
预期结果 ['1','2','3']
我当前的代码如下所示:
var matches = [];
value.replace(fPattern, function (a, s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12, s13, s14, s15) {
for(var i = 1, v; i < 15; i++){
v = eval('s' + i);
if(v){
matches.push(v);
}else{
break;
}
}
});
它有效,但我不喜欢它的工作方式。
第一件事是我实际上不知道我的正则表达式变量中会有多少组 fPattern
所以我需要定义很多不必要的变量 s1, s2 ... etc
.
第二个问题是我决定使用 evil eval
来防止将这个变量 'manually' 一个一个地推入数组,也许有更好的解决方案?
还有一件事 - 我确实尝试使用 match()
但不幸的是,当我有模式 /(1)(2)(3)/g
时,它将 return 我的数组 ['123']
所以这不是我想要的实现。
谢谢!
编辑
好的,我找到了更好看的东西
matches = fPattern.exec(value);
if(matches && matches.length){
for(var key in matches){
if(key !== '0'){
if(key !== 'index'){
formated += matches[key] + ' ';
}else{
break;
}
}
};
}
类似
arrays = "123".match(/(1)(2)(3)/);
arrays.splice(0,1);
console.log(arrays);
=> Array [ "1", "2", "3" ]
match
returns 一个数组,其中数组索引 0
将包含整个匹配项。从数组索引 1
开始,它将包含相应捕获组的值。
arrays.splice(0,1);
会删除索引 0
元素,即数组中的整个匹配项,生成的数组将仅包含捕获组值
使用RegExp.exec
并收集其return值,包括主匹配、捕获组和主匹配的起始索引。
function findall(re, input) {
// Match only once for non global regex
// You are free to modify the code to turn on the global flag
// and turn it off before return
if (!re.global) {
return input.match(re);
} else {
re.lastIndex = 0;
}
var arr;
var out = [];
while ((arr = re.exec(input)) != null) {
delete arr.input; // Don't need this
out.push(arr);
// Empty string match. Need to advance lastIndex
if (arr[0].length == 0) {
re.lastIndex++;
}
}
return out;
}
状态较少/功能较多的解决方案可能是这样的:
function findInString(string, pattern) {
return string.split('').filter(function (element) {
return element.match(pattern)
})
}
接受要搜索的字符串和正则表达式文字,returns 是匹配元素的数组。所以,例如:
var foo = '123asfasff111f6';
findInString(foo, /\d/g)
将 return [ '1', '2', '3', '1', '1', '1', '6' ]
,似乎 就是您要找的东西(?)。(至少,基于以下内容)
e.g. Regex /(1)(2)(3)/g string 123 expected result ['1','2','3']
您可以传入任何您想要的正则表达式文字,它应该作用于数组中的每个项目,return 如果匹配的话。如果你想轻松地推断出 state/might 以后必须重新使用它来匹配不同的模式,我会选择这样的东西。这个问题对我来说有点模糊,所以您的确切需求可能会略有不同——试图偏离您预期的输入和输出。
我正在寻找一种有效的方法来创建包含所有匹配项的数组包含正则表达式组匹配项。
例如正则表达式 /(1)(2)(3)/g
字符串 123
预期结果 ['1','2','3']
我当前的代码如下所示:
var matches = [];
value.replace(fPattern, function (a, s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12, s13, s14, s15) {
for(var i = 1, v; i < 15; i++){
v = eval('s' + i);
if(v){
matches.push(v);
}else{
break;
}
}
});
它有效,但我不喜欢它的工作方式。
第一件事是我实际上不知道我的正则表达式变量中会有多少组 fPattern
所以我需要定义很多不必要的变量 s1, s2 ... etc
.
第二个问题是我决定使用 evil eval
来防止将这个变量 'manually' 一个一个地推入数组,也许有更好的解决方案?
还有一件事 - 我确实尝试使用 match()
但不幸的是,当我有模式 /(1)(2)(3)/g
时,它将 return 我的数组 ['123']
所以这不是我想要的实现。
谢谢!
编辑
好的,我找到了更好看的东西
matches = fPattern.exec(value);
if(matches && matches.length){
for(var key in matches){
if(key !== '0'){
if(key !== 'index'){
formated += matches[key] + ' ';
}else{
break;
}
}
};
}
类似
arrays = "123".match(/(1)(2)(3)/);
arrays.splice(0,1);
console.log(arrays);
=> Array [ "1", "2", "3" ]
match
returns 一个数组,其中数组索引 0
将包含整个匹配项。从数组索引 1
开始,它将包含相应捕获组的值。
arrays.splice(0,1);
会删除索引 0
元素,即数组中的整个匹配项,生成的数组将仅包含捕获组值
使用RegExp.exec
并收集其return值,包括主匹配、捕获组和主匹配的起始索引。
function findall(re, input) {
// Match only once for non global regex
// You are free to modify the code to turn on the global flag
// and turn it off before return
if (!re.global) {
return input.match(re);
} else {
re.lastIndex = 0;
}
var arr;
var out = [];
while ((arr = re.exec(input)) != null) {
delete arr.input; // Don't need this
out.push(arr);
// Empty string match. Need to advance lastIndex
if (arr[0].length == 0) {
re.lastIndex++;
}
}
return out;
}
状态较少/功能较多的解决方案可能是这样的:
function findInString(string, pattern) {
return string.split('').filter(function (element) {
return element.match(pattern)
})
}
接受要搜索的字符串和正则表达式文字,returns 是匹配元素的数组。所以,例如:
var foo = '123asfasff111f6';
findInString(foo, /\d/g)
将 return [ '1', '2', '3', '1', '1', '1', '6' ]
,似乎 就是您要找的东西(?)。(至少,基于以下内容)
e.g. Regex /(1)(2)(3)/g string 123 expected result ['1','2','3']
您可以传入任何您想要的正则表达式文字,它应该作用于数组中的每个项目,return 如果匹配的话。如果你想轻松地推断出 state/might 以后必须重新使用它来匹配不同的模式,我会选择这样的东西。这个问题对我来说有点模糊,所以您的确切需求可能会略有不同——试图偏离您预期的输入和输出。