-1

I'm trying to apply a regular expression to a string, in search of any placeholders, but I can't figure out why the result is only the full match, not the capturing group.

//----HTML------------------------//    
<p>Let's try plaintext.<br/>
bommdidumpidudidoo</p>

<p id="output"></p>

//----JS------------------------//    
var s = $('p').html();
var matches = s.match( /.*(plaintext)/g );

write(matches);
write(matches[0]);
write(matches[1]);



//------- whatever -------//
function write(s) {
    $('#output').html( $('#output').html() +'<br/>'+s );
}

// output:
// Let's try plaintext
// Let's try plaintext
// undefined

» fiddle
(I've used my custom function write instead of console.log so that the result would show up in the fiddle)

Why is the third line undefined? I don't understand!

I'm pretty sure the expression is right. I'm 1oo% certain that this is the right capturing group syntax for JavaScript, and that .match() returns an array with the full match first, then all capturing groups. I've even tested it with regex101.com – over there, I do get the capturing group.

It's not the same as this other problem because there, the OR logic was the crux of the problem and here the pipe | doesn't even come up.

Community
  • 1
  • 1
WoodrowShigeru
  • 1,418
  • 1
  • 18
  • 25
  • That other thread is related but how is it a duplicate? Neither was I asking about quotation marks, nor did any of the answers mark the `g` modifier as the cause for `.match()` not returning capturing groups. – WoodrowShigeru Nov 16 '15 at 11:38

1 Answers1

1

Oooh! It's because I'm doing a global search, with the g modifier. I do get the expected result if I remove that modifier. Tss.

I've used that modifier in the first place in order to grab multiple placeholders, but I guess I can still while-loop that stuff …

WoodrowShigeru
  • 1,418
  • 1
  • 18
  • 25