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

ZEE
  • 2,931
  • 5
  • 35
  • 47
  • 1
    which is the question? you need 3 numbers or 3 letters or something else from the string? – micnic Jun 07 '12 at 21:09

3 Answers3

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

apsillers
  • 112,806
  • 17
  • 235
  • 239
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/>' );
Larry Battle
  • 9,008
  • 4
  • 41
  • 55
  • Ok larry, but note... the capture done by the (.*) in the regex expression should have stripped down the dots... and I should be getting ["000", "111", "222"] and not [".000.", ".111.", ".222."] – ZEE Jun 07 '12 at 21:32
  • Ok... my hope to do it at once vanish... I'll stick to remove the dots after... ;-) – ZEE Jun 08 '12 at 00:07
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.

LeonardChallis
  • 7,759
  • 6
  • 45
  • 76
  • I know that, the question was how to get the capture done by the (.*) in the regex expression – ZEE Jun 07 '12 at 21:30