Javascript-正则表达式查找多个括号匹配

Javascript-正则表达式查找多个括号匹配,javascript,regex,input,replace,split,Javascript,Regex,Input,Replace,Split,所以目前,我的代码适用于包含一组括号的输入 var re = /^.*\((.*\)).*$/; var inPar = userIn.replace(re, '$1'); …这意味着,当用户输入化学式Cu(NO3)2时,提醒inPar返回NO3),这是我想要的 但是,如果输入的是Cu(NO3)2(CO2)3,则只返回CO2) 我对正则表达式不太了解,所以为什么会发生这种情况,有没有一种方法可以在找到NO3)和CO2)后将它们放入数组中?您的正则表达式有锚来匹配字符串的开头和结尾,所以它不足以

所以目前,我的代码适用于包含一组括号的输入

var re = /^.*\((.*\)).*$/;
var inPar = userIn.replace(re, '$1');
…这意味着,当用户输入化学式Cu(NO3)2时,提醒inPar返回NO3),这是我想要的

但是,如果输入的是Cu(NO3)2(CO2)3,则只返回CO2)


我对正则表达式不太了解,所以为什么会发生这种情况,有没有一种方法可以在找到NO3)和CO2)后将它们放入数组中?

您的正则表达式有锚来匹配字符串的开头和结尾,所以它不足以匹配多个实例。使用RegExp
g
标志(全局修饰符)更新代码:

如果您需要旧IE支持:

或不带多边形填充:

var userIn = 'Cu(NO3)2(CO2)3';
var inPar = [];
userIn.replace(/\(([^)]*\))/g, function(s, m) { inPar.push(m); });
inPar; //["NO3)", "CO2)"]
上面匹配一个
并捕获零个或多个非
字符序列,后跟一个
,并将其推送到
inPar
数组

第一个正则表达式本质上是相同的,但是使用整个匹配,包括开头的
括号(稍后通过映射数组将其删除),而不是捕获组


根据问题,我假设结束括号应位于结果字符串中,否则,以下是不带结束括号的更新解决方案:

对于第一种解决方案(使用
s.slice(1,-1)
):

对于第二个解决方案(
\)
捕获组之外):

您希望使用而不是String.replace。您还需要正则表达式匹配括号中的多个字符串,这样就不能有^(字符串的开头)和$(字符串的结尾)。在括号内匹配时,我们不能贪婪,所以我们将使用。*

逐步进行更改,我们得到:

// Use Match
"Cu(NO3)2(CO2)3".match(/^.*\((.*\)).*$/);
["Cu(NO3)2(CO2)3", "CO2)"]

// Lets stop including the ) in our match
"Cu(NO3)2(CO2)3".match(/^.*\((.*)\).*$/);
["Cu(NO3)2(CO2)3", "CO2"]

// Instead of matching the entire string, lets search for just what we want
"Cu(NO3)2(CO2)3".match(/\((.*)\)/);
["(NO3)2(CO2)", "NO3)2(CO2"]

// Oops, we're being a bit too greedy, and capturing everything in a single match
"Cu(NO3)2(CO2)3".match(/\((.*?)\)/);
["(NO3)", "NO3"]

// Looks like we're only searching for a single result. Lets add the Global flag
"Cu(NO3)2(CO2)3".match(/\((.*?)\)/g);
["(NO3)", "(CO2)"]

// Global captures the entire match, and ignore our capture groups, so lets remove them
"Cu(NO3)2(CO2)3".match(/\(.*?\)/g);
["(NO3)", "(CO2)"]

// Now to remove the parentheses. We can use Array.prototype.map for that!
var elements = "Cu(NO3)2(CO2)3".match(/\(.*?\)/g);
elements = elements.map(function(match) { return match.slice(1, -1); })
["NO3", "CO2"]

// And if you want the closing parenthesis as Fabrício Matté mentioned
var elements = "Cu(NO3)2(CO2)3".match(/\(.*?\)/g);
elements = elements.map(function(match) { return match.substr(1); })
["NO3)", "CO2)"]

您可以尝试以下方法:

"Cu(NO3)2".match(/(\S\S\d)/gi)   // returns NO3


"Cu(NO3)2(CO2)3".match(/(\S\S\d)/gi)   // returns NO3  CO2

按钟敲打+1为了从问题中指出匹配而不是替换和全局修饰符,我相信结束语
应该在返回的字符串中。另外,我更喜欢
match.slice(1,-1)
删除起始括号和结束括号,而不是将不必要的正则表达式放入其中。很好的slice调用!我将更新代码以使用它。我不知道为什么Rygh2014会想要),但在你的代码和我的代码之间,这应该是一个相当明显的变化。看起来不错。吹毛求疵,在达成满意的解决方案之前,有太多的代码和注释,但是试图帮助OP理解它+1。
userIn.replace(/\(([^)]*)\)/g, function(s, m) { inPar.push(m); });
// Use Match
"Cu(NO3)2(CO2)3".match(/^.*\((.*\)).*$/);
["Cu(NO3)2(CO2)3", "CO2)"]

// Lets stop including the ) in our match
"Cu(NO3)2(CO2)3".match(/^.*\((.*)\).*$/);
["Cu(NO3)2(CO2)3", "CO2"]

// Instead of matching the entire string, lets search for just what we want
"Cu(NO3)2(CO2)3".match(/\((.*)\)/);
["(NO3)2(CO2)", "NO3)2(CO2"]

// Oops, we're being a bit too greedy, and capturing everything in a single match
"Cu(NO3)2(CO2)3".match(/\((.*?)\)/);
["(NO3)", "NO3"]

// Looks like we're only searching for a single result. Lets add the Global flag
"Cu(NO3)2(CO2)3".match(/\((.*?)\)/g);
["(NO3)", "(CO2)"]

// Global captures the entire match, and ignore our capture groups, so lets remove them
"Cu(NO3)2(CO2)3".match(/\(.*?\)/g);
["(NO3)", "(CO2)"]

// Now to remove the parentheses. We can use Array.prototype.map for that!
var elements = "Cu(NO3)2(CO2)3".match(/\(.*?\)/g);
elements = elements.map(function(match) { return match.slice(1, -1); })
["NO3", "CO2"]

// And if you want the closing parenthesis as Fabrício Matté mentioned
var elements = "Cu(NO3)2(CO2)3".match(/\(.*?\)/g);
elements = elements.map(function(match) { return match.substr(1); })
["NO3)", "CO2)"]
"Cu(NO3)2".match(/(\S\S\d)/gi)   // returns NO3


"Cu(NO3)2(CO2)3".match(/(\S\S\d)/gi)   // returns NO3  CO2