0

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

4

3 に答える 3

5

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.)

于 2012-06-07T21:14:06.533 に答える
0

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/>' );
于 2012-06-07T21:14:22.280 に答える
0

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.

于 2012-06-07T21:15:04.057 に答える