Accessing multiple captures in a RegEx (Newbie thing…)

孤者浪人 提交于 2019-12-08 12:19:03

问题


in this regex multiple capture I have to add "g" flag to obtain all items...

"aaa bbb ccc \n.000.\n \n.111.\n sd555 dsf \n.222.\n ddd ".match( /^.(.*).$/gm )

when I add the "g" (global) flag ?how do access the captured groups... there should be 3 like ["000", "111", "222"] but i don't know how to access them... I keep getting [".000.", ".111.", ".222."] << note the dots before and after the words


回答1:


If you want to get capture groups in a global regex, you can't use match, unfortunately. Instead you'll need to use exec on the regex:

var myregex = /^.(.*).$/gm;
var result, allMatches = [];
while((result = myregex.exec(mystring)) != null) {
    var match = result[1]; // get the first match pattern of this match
    allMatches.push(match);
}

With a global regex, match returns an array of all the whole matches and never returns capture groups. exec returns a single match and all of its capture groups. To get all the matches, you must call exec multiple times until it finally returns null.

Note that exec relies on the regex maintaining state, so you must save the regex in a variable:

while((result = /^.(.*).$/gm.exec(mystring)) != null) // BAD and WRONG!

This is wrong because with each loop, there is a fresh regex which doesn't know what match it is supposed to be returning this loop. (Or, more precisely, it doesn't know the lastIndex of the previous regex.)




回答2:


In FireBug:

var hello = "aaa bbb ccc \n.000.\n \n.111.\n sd555 dsf \n.222.\n ddd ".match( /^.(.*).$/gm );
console.dir(hello);

Then you can use hello[n] where n is the match you want, such as `hello[1].

However, you need to modify your regex if you only want to match certain things.




回答3:


The returned result from str.match( re ) is an array.

Demo here. http://jsfiddle.net/rXgQa/

var re = /^.(.*).$/gm;
var str = "aaa bbb ccc \n.000.\n \n.111.\n sd555 dsf \n.222.\n ddd ";
var matches = str.match( re );
if( matches ){
    document.body.innerHTML += matches.join( '<br/> ' );
}​

Outputs:

aaa bbb ccc // matches[0]
.000.     // matches[1]
.111.     // matches[2]
sd555 dsf // matches[3]
.222.     // matches[4]
ddd       // matches[5]

Update

Here's my answer to the second part of the question. Question: How to get rid of the dot before and after the numbers?

My Answer: I would just loop through the matches and replace the dot with an empty string. Also, your regular expression is wrong since you need to escape the dot.

Updated jsfiddle demo: http://jsfiddle.net/rXgQa/1/

var re = /^\.([^\.]+)\.$/gm;
var lines = "aaa bbb ccc \n.000.\n \n.111.\n sd555 dsf \n.222.\n ddd ";
var matches = lines.match( re );
var i = (matches||[]).length;
while( i-- ){
    matches[i] = matches[i].replace( /\./g, '' );
}

document.body.innerHTML += matches.join( '<br/>' );


来源:https://stackoverflow.com/questions/10939873/accessing-multiple-captures-in-a-regex-newbie-thing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!