Javascript 正则表达式提取子串,由于某种原因返回2个结果
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3486359/
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
Regex to extract substring, returning 2 results for some reason
提问by Rick
I need to do a lot of regex things in javascript but am having some issues with the syntax and I can't seem to find a definitive resource on this.. for some reason when I do:
我需要在 javascript 中做很多正则表达式的事情,但在语法上有一些问题,我似乎无法找到一个明确的资源......出于某种原因,当我这样做时:
var tesst = "afskfsd33j"
var test = tesst.match(/a(.*)j/);
alert (test)
it shows
表明
"afskfsd33j, fskfsd33"
I'm not sure why its giving this output of original and the matched string, I am wondering how I can get it to just give the match (essentially extracting the part I want from the original string)
我不确定为什么它会给出原始和匹配字符串的输出,我想知道如何让它只给出匹配(基本上是从原始字符串中提取我想要的部分)
Thanks for any advice
感谢您的任何建议
回答by Jacob Relkin
matchreturns an array.
match返回一个数组。
The default string representation of an array in JavaScript is the elements of the array separated by commas. In this case the desired result is in the second element of the array:
JavaScript 中数组的默认字符串表示是用逗号分隔的数组元素。在这种情况下,所需的结果在数组的第二个元素中:
var tesst = "afskfsd33j"
var test = tesst.match(/a(.*)j/);
alert (test[1]);
回答by BillP3rd
I think your problem is that the match method is returning an array. The 0th item in the array is the original string, the 1st thru nth items correspond to the 1st through nth matched parenthesised items. Your "alert()" call is showing the entire array.
我认为您的问题是 match 方法正在返回一个数组。数组中的第 0 项是原始字符串,第 1 到第 n 项对应于第 1 到第 n 个匹配的括号项。您的“alert()”调用显示了整个数组。
回答by Jan Stanicek
Each group defined by parenthesis () is captured during processing and each captured group content is pushed into result array in same order as groups within pattern starts. See more on http://www.regular-expressions.info/brackets.htmland http://www.regular-expressions.info/refcapture.html(choose right language to see supported features)
括号 () 定义的每个组在处理过程中被捕获,每个捕获的组内容按与模式中的组开始的顺序相同的顺序推送到结果数组中。在http://www.regular-expressions.info/brackets.html和http://www.regular-expressions.info/refcapture.html上查看更多信息(选择正确的语言以查看支持的功能)
var source = "afskfsd33j"
var result = source.match(/a(.*)j/);
result: ["afskfsd33j", "fskfsd33"]
The reason why you received this exact result is following:
您收到此确切结果的原因如下:
First value in array is the first found string which confirms the entire pattern. So it should definitely start with "a" followed by any number of any characters and ends with first "j" char after starting "a".
数组中的第一个值是第一个找到的字符串,它确认了整个模式。因此,它绝对应该以“a”开头,后跟任意数量的任何字符,并以“a”开头后的第一个“j”字符结尾。
Second value in array is captured group defined by parenthesis. In your case group contain entire pattern match without content defined outside parenthesis, so exactly "fskfsd33".
数组中的第二个值是由括号定义的捕获组。在您的案例组中,包含完整的模式匹配,而没有在括号外定义内容,因此正好是“fskfsd33”。
If you want to get rid of second value in array you may define pattern like this:
如果你想摆脱数组中的第二个值,你可以像这样定义模式:
/a(?:.*)j/
where "?:" means that group of chars which match the content in parenthesis will not be part of resulting array.
其中“?:”表示与括号中的内容匹配的字符组将不是结果数组的一部分。
Other options might be in this simple case to write pattern without any group because it is not necessary to use group at all:
在这种简单的情况下,其他选项可能是在没有任何组的情况下编写模式,因为根本不需要使用组:
/a.*j/
If you want to just check whether source text matches the pattern and does not care about which text it found than you may try:
如果您只想检查源文本是否与模式匹配,而不关心它找到了哪个文本,那么您可以尝试:
var result = /a.*j/.test(source);
The result should return then only true|false values. For more info see http://www.javascriptkit.com/javatutors/re3.shtml
结果应该只返回 true|false 值。有关更多信息,请参阅http://www.javascriptkit.com/javatutors/re3.shtml
回答by Ekrami
Just get rid of the parenthesis and that will give you an array with one element and:
只需去掉括号,这将为您提供一个包含一个元素的数组,并且:
- Change this line
- 改变这一行
var test = tesst.match(/a(.*)j/);
var test = test.match(/a(.*)j/);
- To this
- 对此
var test = tesst.match(/a.*j/);
var test = test.match(/a.*j/);
If you add parenthesis the match() function will find two match for you one for whole expression and one for the expression inside the parenthesis
如果添加括号,match() 函数将为您找到两个匹配项,一个用于整个表达式,另一个用于括号内的表达式
- Also according to developer.mozilla.org docs :
- 同样根据 developer.mozilla.org 文档:
If you only want the first match found, you might want to use RegExp.exec() instead.
如果您只想找到第一个匹配项,您可能需要改用 RegExp.exec()。
You can use the below code:
您可以使用以下代码:
RegExp(/a.*j/).exec("afskfsd33j")
RegExp(/a.*j/).exec("afskfsd33j")
回答by techturbulence
I've just had the same problem.
我刚刚遇到了同样的问题。
You only get the text twice in your result if you include a match group (in brackets) and the 'g' (global) modifier. The first item always is the first result, normally OK when using match(reg) on a short string, however when using a construct like:
如果包含匹配组(括号中)和 'g'(全局)修饰符,则结果中只会出现两次文本。第一项始终是第一个结果,在短字符串上使用 match(reg) 时通常可以,但是当使用如下结构时:
while ((result = reg.exec(string)) !== null){
console.log(result);
}
the results are a little different.
结果有点不同。
Try the following code:
试试下面的代码:
var regEx = new RegExp('([0-9]+ (cat|fish))','g'), sampleString="1 cat and 2 fish";
var result = sample_string.match(regEx);
console.log(JSON.stringify(result));
// ["1 cat","2 fish"]
var reg = new RegExp('[0-9]+ (cat|fish)','g'), sampleString="1 cat and 2 fish";
while ((result = reg.exec(sampleString)) !== null) {
console.dir(JSON.stringify(result))
};
// '["1 cat","cat"]'
// '["2 fish","fish"]'
var reg = new RegExp('([0-9]+ (cat|fish))','g'), sampleString="1 cat and 2 fish";
while ((result = reg.exec(sampleString)) !== null){
console.dir(JSON.stringify(result))
};
// '["1 cat","1 cat","cat"]'
// '["2 fish","2 fish","fish"]'
(tested on recent V8 - Chrome, Node.js)
(在最近的 V8 上测试 - Chrome、Node.js)
The best answer is currently a comment which I can't upvote, so credit to @Mic.
目前最好的答案是我无法投票的评论,因此请归功于@Mic。

