我正在使用Javascript和正则表达式解析“csv like flavor”中的一些字符串,其中;
为分隔符。我到目前为止所发现的正则表达式试图让所有出现的模式如:“INTERESTING1 (INTERESTING2; INTERESTING3)
”。
我面临的问题是我只能匹配整个字符串中最后一个出现的模式,而我想匹配所有出现的模式。我已经尝试了Javascript函数exec()
和match()
,有或没有一些循环,但我无法弄清楚我在做什么有什么问题?
var complexString = 'some boring stuff; some other boring stuff; interesting prefix (interesting inner stuff1; interesting inner stuff2; etc.); boring stuff; another interesting prefix (another interesting string 1; another interesting string 2; etc.)';
//var complexString = 'XXX';
// regex to apply
var roundBraketsRegex = /.*;(.*)\((.*)\)/g; // string pattern: "INTERESTING1 (INTERESTING2; INTERESTING3)"
// array of matched groups
var matchesArray = roundBraketsRegex.exec(complexString);
var outputString = '';
if(matchesArray == null ) {
outputString = 'NULL!!! ';
}
// I have tried also the following commented line with stuff related to
// while loops and functions like .exec() or .match()
//while ((matchesArray = roundBraketsRegex.match( complexString )) != null) {
outputString = outputString + ' ### ' + matchesArray[1] + ' ### ' + matchesArray[2] + ' ### NOT INTERESTED IN: ' + matchesArray[0];
//}
// print what has been found
console.log(document.getElementById('result'));
document.getElementById('result').innerHTML = outputString;
输出(我在Stackoverflow中手动添加了一些回车,只是为了让字符串更具可读性):
### another interesting prefix
### another interesting string 1; another interesting string 2; etc.
### NOT INTERESTED IN: some boring stuff; some other boring stuff; interesting prefix (interesting inner stuff1; interesting inner stuff2; etc.); boring stuff; another interesting prefix (another interesting string 1; another interesting string 2; etc.)
答案 0 :(得分:1)
您需要了解正则表达式的事情是,匹配器的多次运行只会找到非重叠目标。如果你的正则表达式捕获太多,那么你将无法通过额外的运行找到额外的匹配。
尝试使用这个捕获较少的正则表达式:
([^;]+?)\s+\(([^\)]*)\)
它有两个捕获组,它们在括号中抓取有趣的前缀和其他有趣的东西。请注意,您需要在结果上使用String.trim()。这是在Regex 101上解释的正则表达式。
这是最终的JavaScript解决方案,其中包括正则表达式:
var complexString = 'some boring stuff; some other boring stuff; interesting prefix (interesting inner stuff1; interesting inner stuff2; etc.); boring stuff; another interesting prefix (another interesting string 1; another interesting string 2; etc.)';
var roundBraketsRegex = /([^;]+?)\s+\(([^\)]*)\)/g;
var matchesArray;
var i = 1;
while (matchesArray = roundBraketsRegex.exec(complexString)) {
var group1 = matchesArray[1].trim();
var group2 = matchesArray[2].trim();
console.log("Match #" + i + " [1]: '" + group1 + "' [2]: '" + group2 + "'");
++i;
}
以下是运行上述内容的输出:
Match #1 [1]: 'interesting prefix' [2]: 'interesting inner stuff1; interesting inner stuff2; etc.'
Match #2 [1]: 'another interesting prefix' [2]: 'another interesting string 1; another interesting string 2; etc.'
我希望你觉得这很有帮助。
- 乔纳森