目前,我的代码适用于包含一个括号集的输入。
var re = /^.*\((.*\)).*$/;
var inPar = userIn.replace(re, '$1');
...这意味着当用户输入化学式Cu(NO3)2时,alerting inPar会返回NO3),这正是我想要的。
然而,如果输入的是Cu(NO3)2(CO2)3,则只返回CO2)。
我对RegEx不太熟悉,所以为什么会发生这种情况,有没有办法在找到它们后将NO3)和CO2)放入数组中?
目前,我的代码适用于包含一个括号集的输入。
var re = /^.*\((.*\)).*$/;
var inPar = userIn.replace(re, '$1');
...这意味着当用户输入化学式Cu(NO3)2时,alerting inPar会返回NO3),这正是我想要的。
然而,如果输入的是Cu(NO3)2(CO2)3,则只返回CO2)。
我对RegEx不太熟悉,所以为什么会发生这种情况,有没有办法在找到它们后将NO3)和CO2)放入数组中?
// 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)"]
你的正则表达式有锚点来匹配字符串的开头和结尾,所以它不能满足匹配多个出现的要求。更新后的代码使用 String.match
和 RegExp g
标志(全局修饰符):
var userIn = 'Cu(NO3)2(CO2)3';
var inPar = userIn.match(/\([^)]*\)/g).map(function(s){ return s.substr(1); });
inPar; //["NO3)", "CO2)"]
如果您需要支持旧版IE:
或者不使用polyfills:
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)
):
var inPar = userIn.match(/\([^)]*\)/g).map(function(s){ return s.slice(1, -1);});
对于第二种解决方案(\)
在捕获组之外):
userIn.replace(/\(([^)]*)\)/g, function(s, m) { inPar.push(m); });
"Cu(NO3)2".match(/(\S\S\d)/gi) // returns NO3
"Cu(NO3)2(CO2)3".match(/(\S\S\d)/gi) // returns NO3 CO2
)
。此外,我更喜欢使用match.slice(1, -1)
来删除起始和结束括号,而不是将不必要的正则表达式放入其中。 - Fabrício Matté