javascript javascript中的正则表达式匹配

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9901836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 08:10:35  来源:igfitidea点击:

Regular Expression match in javascript

javascriptregex

提问by Jaison Justus

I have the string SUM([A2:A10],[B2:B10],[C2:C10],[D2:D10])and I need to get the elements [A2:A10],[B2:B10],[C2:C10],[D2:D10]in an array, so I used match()in js. The code snippet is

我有字符串SUM([A2:A10],[B2:B10],[C2:C10],[D2:D10]),我需要获取[A2:A10],[B2:B10],[C2:C10],[D2:D10]数组中的元素,所以我match()在js中使用。代码片段是

var formula = "SUM([A2:A10],[B2:B10],[C2:C10],[D2:D10])";
var reg = /\[(a-zA-Z0-9)+\]/;
matches = formula.match(reg);

But I am not getting the match. I hope the regular expression is wrong. I am very poor in building regular expression. What will be the correct regular expression?

但我没有得到比赛。我希望正则表达式是错误的。我在构建正则表达式方面很差。什么是正确的正则表达式?

回答by Another Code

Try it like this:

像这样尝试:

var formula = 'SUM([A2:A10],[B2:B10],[C2:C10],[D2:D10])';
var reg = /\[\w+:\w+\]/g;
matches = formula.match(reg);

Output:

输出:

["[A2:A10]", "[B2:B10]", "[C2:C10]", "[D2:D10]"]

Your regex was in the right direction, but didn't include the colon and captured individual characters. The \wescape sequence I used is a shortcut for a word character ([a-zA-Z0-9_]), makes it more readable. The gflag is necessary to get all matches instead of just the first one.

您的正则表达式方向正确,但不包括冒号和捕获的单个字符。\w我使用的转义序列是单词字符 ( [a-zA-Z0-9_])的快捷方式,使其更具可读性。该g标志是获取所有匹配项而不是第一个匹配项所必需的。

回答by jfriend00

var str = "SUM([A2:A10],[B2:B10],[C2:C10],[D2:D10])";

var matches = str.match(/\[[A-Z0-9:]+\]/g);
alert(matches);?

Note, you use the gflag on the regex to get all the matches.

请注意,您使用g正则表达式上的标志来获取所有匹配项。

You can see it working here: http://jsfiddle.net/jfriend00/aTrLU/

你可以在这里看到它的工作:http: //jsfiddle.net/jfriend00/aTrLU/

回答by Chetter Hummin

The regex you need is

您需要的正则表达式是

/\[[A-Z][0-9]+:[A-Z][0-9]+\]/gi

The g modifier indicates that we should do a global mtching and return all the results, not just the first

g 修饰符表示我们应该进行全局匹配并返回所有结果,而不仅仅是第一个

The i modifier indicates that we need a case-insensitive matching

i 修饰符表示我们需要不区分大小写的匹配

An assumption made is that you don't want to include [E2:E10:], because of extra semicolon

所做的假设是您不想包含 [E2:E10:],因为有额外的分号

回答by Diode

var formula = "SUM([A2:A10],[B2:B10],[C2:C10],[D2:D10])";
var reg = /\[.*?\]/g;
matches = formula.match(reg);

回答by Has QUIT--Anony-Mousse

Your regexp doesn't include the colon :.

您的正则表达式不包括冒号:

Try this: /\[([0-9A-Z:]+)\]/, in particular why I have quoted and unquoted square brackets there.

试试这个: /\[([0-9A-Z:]+)\]/,特别是为什么我在那里引用了和未引用的方括号。

Plus, your input string isn't quoted.

另外,您的输入字符串没有被引用。

回答by ZER0

I believe you forgot the quotes. Also, what you need is also the global flag. Here the code:

我相信你忘记了引号。此外,您还需要全局标志。这里的代码:

var formula = "SUM([A2:A10],[B2:B10],[C2:C10],[D2:D10])";
var reg = /\[[0-z]+:[0-z]+\]/g;
var matches = formula.match(reg);