Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/3.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 正则表达式提取带括号的文本(一个字符串中有多个)_Javascript_Regex - Fatal编程技术网

Javascript 正则表达式提取带括号的文本(一个字符串中有多个)

Javascript 正则表达式提取带括号的文本(一个字符串中有多个),javascript,regex,Javascript,Regex,我想提取文本,后跟多个一对一字符串的括号 示例: text(text1) password(password1) in table(table_name) with some random text 所以,我想在表中提取它们,如下所示: COL1 COL2 ------------------- text text1 password password1 table table_name 所以在表中,我指的是使用它们并在需要时给它

我想提取文本,后跟多个一对一字符串的括号

示例:

text(text1) password(password1) in table(table_name) with some random text
所以,我想在表中提取它们,如下所示:

COL1          COL2
-------------------
text          text1
password      password1
table         table_name
所以在表中,我指的是使用它们并在需要时给它们打电话的可能性

我所尝试的:

这个正则表达式只允许我提取第一个括号,但不包括“文本”,这不是我想要的:

"text(text1) password(password1) in table(table_name) with some random text".match(/\(([^)]+)\)/)[1]
返回:

text1
我希望“文本”将包含在正则表达式中,如本文顶部的示例所述


提前感谢。

您可以这样做:

COL1          COL2
-------------------
text          text1
password      password1
table         table_name
var str=“带有一些随机文本的表(表名称)中的文本(text1)密码(password1)”;
var exp=new RegExp(“([a-z]+)\\([^)]+)\\”,“ig”);
变量组=[]
变量匹配
while(true){
匹配项=exp.exec(str)
如果(!匹配){
打破
}
组。推送([匹配项[1],匹配项[2]])
}

console.log(组)
您可以执行以下操作:

COL1          COL2
-------------------
text          text1
password      password1
table         table_name
var str=“带有一些随机文本的表(表名称)中的文本(text1)密码(password1)”;
var exp=new RegExp(“([a-z]+)\\([^)]+)\\”,“ig”);
变量组=[]
变量匹配
while(true){
匹配项=exp.exec(str)
如果(!匹配){
打破
}
组。推送([匹配项[1],匹配项[2]])
}

console.log(groups)
第二部分的正则表达式可以,但应该使用而不是匹配,并使用
/g
(全局)标志

对于第一个捕获组,您无法匹配空白字符
\S

const regex=/(\S+)\([^)]+)\)/g;
const str=`text(text1)密码(password1)在表(table_name)中带有一些随机文本`;
让m;
while((m=regex.exec(str))!==null){
if(m.index==regex.lastIndex){
regex.lastIndex++;
}
log(m[1],m[2]);

}
第二部分的正则表达式可以,但您应该使用而不是匹配,并使用
/g
(全局)标志

对于第一个捕获组,您无法匹配空白字符
\S

const regex=/(\S+)\([^)]+)\)/g;
const str=`text(text1)密码(password1)在表(table_name)中带有一些随机文本`;
让m;
while((m=regex.exec(str))!==null){
if(m.index==regex.lastIndex){
regex.lastIndex++;
}
log(m[1],m[2]);

}
让reg=/(\w+)\([^)]+)\/g,结果=[],当前匹配;while((currentMatch=reg.exec(txt))!==null){result.push({name:currentMatch[1],param:currentMatch[2]});}
@ASDFGerte谢谢!最好的答案是肯定的。
let reg=/(\w+)\([^)]+)\/g,result=[],currentMatch;while((currentMatch=reg.exec(txt))!==null){result.push({name:currentMatch[1],param:currentMatch[2]});}
@ASDFGerte谢谢!这是最好的答案是的。最好和完整的答案,非常感谢你的努力!最好完整的答案,非常感谢您的努力!