用于匹配括号中值的正则表达式/ Javascript

时间:2022-04-13 23:34:36

My web application needs to parse numeric ranges in strings that are enclosed by parenthesis. I've never really understood regex properly so I need some assistance. The code below is kind of what I'm looking to do (I'll then split the string on the hyphen and get the min/max values). Obviously the pattern is wrong - the example below alerts "(10-12) foo (5-10) bar" when my desired result is 1 alert saying (10-12) and the next saying (5-10), or better yet those values without the parenthesis if that's possible.

我的Web应用程序需要解析括号括起来的字符串中的数值范围。我从来没有真正理解正则表达式,所以我需要一些帮助。下面的代码是我想要做的事情(然后我会在连字符上拆分字符串并获取最小/最大值)。显然这种模式是错误的 - 下面的例子警告“(10-12)foo(5-10)bar”当我想要的结果是1个警告说(10-12)和下一个说法(5-10),或者更好那些没有括号的值,如果可能的话。

Any assistance is appreciated.

任何帮助表示赞赏。

var string = "foo bar (10-12) foo (5-10) bar";
var pattern = /\(.+\)/gi;
matches = string.match(pattern);

for (var i in matches) {
    alert(matches[i]);
}

2 个解决方案

#1


7  

Make your quantifier lazy by adding a ? after the +. Otherwise, it will greedily consume as much as possible, from your opening ( to the last ) in the string.

通过添加?使你的量词变得懒惰? +之后。否则,它将从字符串的开头(到最后一个)贪婪地消耗掉。

var string = "foo bar (10-12) foo (5-10) bar",
    pattern = /\(.+?\)/g,
    matches = string.match(pattern);

jsFiddle.

If you don't want to include the parenthesis in your matches, generally you'd use a positive lookahead and lookbehind for parenthesis. JavaScript doesn't support lookbehinds (though you can fake them). So, use...

如果您不想在匹配中包括括号,通常您会使用正向前瞻和后视括号。 JavaScript不支持lookbehinds(虽然你可以伪造它们)。所以,用...

var string = "foo bar (10-12) foo (5-10) bar",
    pattern = /\((.+?)\)/g,
    match,
    matches = [];

while (match = pattern.exec(string)) {
    matches.push(match[1]);
}

jsFiddle.

Also...

  • You don't need the i flag in your regex; you don't match any letters.
  • 你的正则表达式中不需要i标志;你不匹配任何字母。

  • You should always scope your variables with var. In your example, matches will be global.
  • 您应该始终使用var来调整变量的范围。在您的示例中,匹配将是全局的。

  • You shouldn't use a for (in) to iterate over an array. You should also check that match() doesn't return null (if no results were found).
  • 您不应该使用for(in)迭代数组。您还应检查match()不返回null(如果未找到结果)。

#2


3  

The problem is that your regular expression is Greedy, meaning that it will match the first bracket and keep on going till it finds the last bracket. To fix this you have two options, you either add the ? symbol after the + to make it non greedy: \(.+?\) or else, match any character except a closing bracket: \([^)]+\).

问题是你的正则表达式是贪婪的,这意味着它将匹配第一个括号并继续前进,直到它找到最后一个括号。要解决此问题,您有两个选择,您要么添加? +之后的符号使其非贪婪:\(。+?\)或者,匹配除结束括号之外的任何字符:\([^]] + \)。

Both of these regular expressions should do what you need.

这两个正则表达式都应该满足您的需求。

#1


7  

Make your quantifier lazy by adding a ? after the +. Otherwise, it will greedily consume as much as possible, from your opening ( to the last ) in the string.

通过添加?使你的量词变得懒惰? +之后。否则,它将从字符串的开头(到最后一个)贪婪地消耗掉。

var string = "foo bar (10-12) foo (5-10) bar",
    pattern = /\(.+?\)/g,
    matches = string.match(pattern);

jsFiddle.

If you don't want to include the parenthesis in your matches, generally you'd use a positive lookahead and lookbehind for parenthesis. JavaScript doesn't support lookbehinds (though you can fake them). So, use...

如果您不想在匹配中包括括号,通常您会使用正向前瞻和后视括号。 JavaScript不支持lookbehinds(虽然你可以伪造它们)。所以,用...

var string = "foo bar (10-12) foo (5-10) bar",
    pattern = /\((.+?)\)/g,
    match,
    matches = [];

while (match = pattern.exec(string)) {
    matches.push(match[1]);
}

jsFiddle.

Also...

  • You don't need the i flag in your regex; you don't match any letters.
  • 你的正则表达式中不需要i标志;你不匹配任何字母。

  • You should always scope your variables with var. In your example, matches will be global.
  • 您应该始终使用var来调整变量的范围。在您的示例中,匹配将是全局的。

  • You shouldn't use a for (in) to iterate over an array. You should also check that match() doesn't return null (if no results were found).
  • 您不应该使用for(in)迭代数组。您还应检查match()不返回null(如果未找到结果)。

#2


3  

The problem is that your regular expression is Greedy, meaning that it will match the first bracket and keep on going till it finds the last bracket. To fix this you have two options, you either add the ? symbol after the + to make it non greedy: \(.+?\) or else, match any character except a closing bracket: \([^)]+\).

问题是你的正则表达式是贪婪的,这意味着它将匹配第一个括号并继续前进,直到它找到最后一个括号。要解决此问题,您有两个选择,您要么添加? +之后的符号使其非贪婪:\(。+?\)或者,匹配除结束括号之外的任何字符:\([^]] + \)。

Both of these regular expressions should do what you need.

这两个正则表达式都应该满足您的需求。